cs11dotnet7/vscode/Chapter05/PacktLibraryNetStandard2/Person.cs

104 lines
2.1 KiB
C#
Raw Normal View History

2022-02-20 18:22:06 +01:00
namespace Packt.Shared; // file-scoped namespace
public partial class Person : object
{
// fields
public string? Name;
public DateTime DateOfBirth;
public WondersOfTheAncientWorld FavoriteAncientWonder;
public WondersOfTheAncientWorld BucketList;
public List<Person> Children = new();
// constants
public const string Species = "Homo Sapiens";
// read-only fields
public readonly string HomePlanet = "Earth";
public readonly DateTime Instantiated;
// constructors
public Person()
{
// set default values for fields
// including read-only fields
Name = "Unknown";
Instantiated = DateTime.Now;
}
public Person(string initialName, string homePlanet)
{
Name = initialName;
HomePlanet = homePlanet;
Instantiated = DateTime.Now;
}
// methods
public void WriteToConsole()
{
WriteLine($"{Name} was born on a {DateOfBirth:dddd}.");
}
public string GetOrigin()
{
return $"{Name} was born on {HomePlanet}.";
}
public (string, int) GetFruit()
{
return ("Apples", 5);
}
public (string Name, int Number) GetNamedFruit()
{
return (Name: "Apples", Number: 5);
}
// deconstructors
public void Deconstruct(out string? name, out DateTime dob)
{
name = Name;
dob = DateOfBirth;
}
public void Deconstruct(out string? name,
out DateTime dob, out WondersOfTheAncientWorld fav)
{
name = Name;
dob = DateOfBirth;
fav = FavoriteAncientWonder;
}
public string SayHello()
{
return $"{Name} says 'Hello!'";
}
public string SayHello(string name)
{
return $"{Name} says 'Hello, {name}!'";
}
public string OptionalParameters(string command = "Run!",
double number = 0.0, bool active = true)
{
return string.Format(
format: "command is {0}, number is {1}, active is {2}",
arg0: command,
arg1: number,
arg2: active);
}
public void PassingParameters(int x, ref int y, out int z)
{
// out parameters cannot have a default
// AND must be initialized inside the method
z = 99;
// increment each parameter
x++;
y++;
z++;
}
}