mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-18 12:43:55 +00:00
Initial commit
This commit is contained in:
parent
10cceacca6
commit
bc96ccb183
22 changed files with 1283 additions and 0 deletions
8
vscode/Chapter05/PacktLibrary/BankAccount.cs
Normal file
8
vscode/Chapter05/PacktLibrary/BankAccount.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
public class BankAccount
|
||||
{
|
||||
public string? AccountName; // instance member
|
||||
public decimal Balance; // instance member
|
||||
public static decimal InterestRate; // shared member
|
||||
}
|
||||
8
vscode/Chapter05/PacktLibrary/Book.cs
Normal file
8
vscode/Chapter05/PacktLibrary/Book.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
public class Book
|
||||
{
|
||||
// required is not working in .NET 7 Preview 1
|
||||
public /* required */ string? Isbn { get; set; }
|
||||
public string? Title { get; set; }
|
||||
}
|
||||
29
vscode/Chapter05/PacktLibrary/FlightPatterns.cs
Normal file
29
vscode/Chapter05/PacktLibrary/FlightPatterns.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
public class BusinessClassPassenger
|
||||
{
|
||||
public override string ToString()
|
||||
{
|
||||
return "Business Class";
|
||||
}
|
||||
}
|
||||
|
||||
public class FirstClassPassenger
|
||||
{
|
||||
public int AirMiles { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"First Class with {AirMiles:N0} air miles";
|
||||
}
|
||||
}
|
||||
|
||||
public class CoachClassPassenger
|
||||
{
|
||||
public double CarryOnKG { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Coach Class with {CarryOnKG:N2} KG carry on";
|
||||
}
|
||||
}
|
||||
14
vscode/Chapter05/PacktLibrary/PacktLibrary.csproj
Normal file
14
vscode/Chapter05/PacktLibrary/PacktLibrary.csproj
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>10</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
103
vscode/Chapter05/PacktLibrary/Person.cs
Normal file
103
vscode/Chapter05/PacktLibrary/Person.cs
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
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++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
153
vscode/Chapter05/PacktLibrary/PersonAutoGen.cs
Normal file
153
vscode/Chapter05/PacktLibrary/PersonAutoGen.cs
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
// this file simulates an autogenerated class
|
||||
|
||||
public partial class Person
|
||||
{
|
||||
// a readonly property defined using C# 1 - 5 syntax
|
||||
public string Origin
|
||||
{
|
||||
get
|
||||
{
|
||||
return string.Format("{0} was born on {1}",
|
||||
arg0: Name, arg1: HomePlanet);
|
||||
}
|
||||
}
|
||||
|
||||
// two readonly properties defined using C# 6+ lambda expression body syntax
|
||||
public string Greeting => $"{Name} says 'Hello!'";
|
||||
public int Age => DateTime.Today.Year - DateOfBirth.Year;
|
||||
|
||||
// a read-write property defined using C# 3.0 syntax
|
||||
public string? FavoriteIceCream { get; set; } // auto-syntax
|
||||
|
||||
// a private field to store the property value
|
||||
private string? favoritePrimaryColor;
|
||||
|
||||
// a public property to read and write to the field
|
||||
public string? FavoritePrimaryColor
|
||||
{
|
||||
get
|
||||
{
|
||||
return favoritePrimaryColor;
|
||||
}
|
||||
set
|
||||
{
|
||||
switch (value?.ToLower())
|
||||
{
|
||||
case "red":
|
||||
case "green":
|
||||
case "blue":
|
||||
favoritePrimaryColor = value;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException(
|
||||
$"{value} is not a primary color. " +
|
||||
"Choose from: red, green, blue.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// indexers
|
||||
public Person this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
return Children[index]; // pass on to the List<T> indexer
|
||||
}
|
||||
set
|
||||
{
|
||||
Children[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public Person this[string name]
|
||||
{
|
||||
get
|
||||
{
|
||||
return Children.Find(p => p.Name == name);
|
||||
}
|
||||
set
|
||||
{
|
||||
Person found = Children.Find(p => p.Name == name);
|
||||
if (found is not null) found = value;
|
||||
}
|
||||
}
|
||||
|
||||
private bool married = false;
|
||||
public bool Married => married;
|
||||
|
||||
private Person? spouse = null;
|
||||
public Person? Spouse => spouse;
|
||||
|
||||
// static method to marry
|
||||
public static void Marry(Person p1, Person p2)
|
||||
{
|
||||
p1.Marry(p2);
|
||||
}
|
||||
|
||||
// instance method to marry
|
||||
public void Marry(Person partner)
|
||||
{
|
||||
if (married) return;
|
||||
spouse = partner;
|
||||
married = true;
|
||||
partner.Marry(this);
|
||||
}
|
||||
|
||||
// static method to "multiply"
|
||||
public static Person Procreate(Person p1, Person p2)
|
||||
{
|
||||
if (p1.Spouse != p2)
|
||||
{
|
||||
throw new ArgumentException("You must be married to procreate.");
|
||||
}
|
||||
|
||||
Person baby = new()
|
||||
{
|
||||
Name = $"Baby of {p1.Name} and {p2.Name}",
|
||||
DateOfBirth = DateTime.Now
|
||||
};
|
||||
|
||||
p1.Children.Add(baby);
|
||||
p2.Children.Add(baby);
|
||||
|
||||
return baby;
|
||||
}
|
||||
|
||||
// instance method to "multiply"
|
||||
public Person ProcreateWith(Person partner)
|
||||
{
|
||||
return Procreate(this, partner);
|
||||
}
|
||||
|
||||
// operator to "marry"
|
||||
public static bool operator +(Person p1, Person p2)
|
||||
{
|
||||
Marry(p1, p2);
|
||||
return p1.Married && p2.Married; // confirm they are both now married
|
||||
}
|
||||
|
||||
// operator to "multiply"
|
||||
public static Person operator *(Person p1, Person p2)
|
||||
{
|
||||
return Procreate(p1, p2);
|
||||
}
|
||||
|
||||
// method with a local function
|
||||
public static int Factorial(int number)
|
||||
{
|
||||
if (number < 0)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"{nameof(number)} cannot be less than zero.");
|
||||
}
|
||||
return localFactorial(number);
|
||||
|
||||
int localFactorial(int localNumber) // local function
|
||||
{
|
||||
if (localNumber == 0) return 1;
|
||||
return localNumber * localFactorial(localNumber - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
7
vscode/Chapter05/PacktLibrary/Records.cs
Normal file
7
vscode/Chapter05/PacktLibrary/Records.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
public class ImmutablePerson
|
||||
{
|
||||
//public string? FirstName { get; init; }
|
||||
//public string? LastName { get; init; }
|
||||
}
|
||||
14
vscode/Chapter05/PacktLibrary/WondersOfTheAncientWorld.cs
Normal file
14
vscode/Chapter05/PacktLibrary/WondersOfTheAncientWorld.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
[Flags]
|
||||
public enum WondersOfTheAncientWorld : byte
|
||||
{
|
||||
None = 0b_0000_0000, // i.e. 0
|
||||
GreatPyramidOfGiza = 0b_0000_0001, // i.e. 1
|
||||
HangingGardensOfBabylon = 0b_0000_0010, // i.e. 2
|
||||
StatueOfZeusAtOlympia = 0b_0000_0100, // i.e. 4
|
||||
TempleOfArtemisAtEphesus = 0b_0000_1000, // i.e. 8
|
||||
MausoleumAtHalicarnassus = 0b_0001_0000, // i.e. 16
|
||||
ColossusOfRhodes = 0b_0010_0000, // i.e. 32
|
||||
LighthouseOfAlexandria = 0b_0100_0000 // i.e. 64
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue