mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
Initial commit
This commit is contained in:
parent
db1136abc1
commit
69eacee2fc
|
|
@ -1,29 +1,34 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
public class BusinessClassPassenger
|
||||
public class Passenger
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public class BusinessClassPassenger : Passenger
|
||||
{
|
||||
public override string ToString()
|
||||
{
|
||||
return "Business Class";
|
||||
return $"Business Class: {Name}";
|
||||
}
|
||||
}
|
||||
|
||||
public class FirstClassPassenger
|
||||
public class FirstClassPassenger : Passenger
|
||||
{
|
||||
public int AirMiles { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"First Class with {AirMiles:N0} air miles";
|
||||
return $"First Class with {AirMiles:N0} air miles: {Name}";
|
||||
}
|
||||
}
|
||||
|
||||
public class CoachClassPassenger
|
||||
public class CoachClassPassenger : Passenger
|
||||
{
|
||||
public double CarryOnKG { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Coach Class with {CarryOnKG:N2} KG carry on";
|
||||
return $"Coach Class with {CarryOnKG:N2} KG carry on: {Name}";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ public partial class Person
|
|||
if (married) return;
|
||||
spouse = partner;
|
||||
married = true;
|
||||
partner.Marry(this);
|
||||
partner.Marry(this); // this is the current object
|
||||
}
|
||||
|
||||
// static method to "multiply"
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ WriteLine($"{thing2.Name} has {thing2.Count} children.");
|
|||
WriteLine($"Deconstructed: {fruitName}, {fruitNumber}");
|
||||
|
||||
// Deconstructing a Person
|
||||
var (name1, dob1) = bob;
|
||||
var (name1, dob1) = bob; // implicitly calls the Deconstruct method
|
||||
WriteLine($"Deconstructed: {name1}, {dob1}");
|
||||
|
||||
var (name2, dob2, fav2) = bob;
|
||||
|
|
@ -227,12 +227,12 @@ WriteLine($"5! is {Person.Factorial(5)}");
|
|||
|
||||
// Pattern matching with objects
|
||||
|
||||
object[] passengers = {
|
||||
new FirstClassPassenger { AirMiles = 1_419 },
|
||||
new FirstClassPassenger { AirMiles = 16_562 },
|
||||
new BusinessClassPassenger(),
|
||||
new CoachClassPassenger { CarryOnKG = 25.7 },
|
||||
new CoachClassPassenger { CarryOnKG = 0 },
|
||||
Passenger[] passengers = {
|
||||
new FirstClassPassenger { AirMiles = 1_419, Name = "Suman" },
|
||||
new FirstClassPassenger { AirMiles = 16_562, Name = "Lucy" },
|
||||
new BusinessClassPassenger { Name = "Janice" },
|
||||
new CoachClassPassenger { CarryOnKG = 25.7, Name = "Dave" },
|
||||
new CoachClassPassenger { CarryOnKG = 0, Name = "Amit" },
|
||||
};
|
||||
|
||||
foreach (object passenger in passengers)
|
||||
|
|
|
|||
|
|
@ -1,29 +1,34 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
public class BusinessClassPassenger
|
||||
public class Passenger
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public class BusinessClassPassenger : Passenger
|
||||
{
|
||||
public override string ToString()
|
||||
{
|
||||
return "Business Class";
|
||||
return $"Business Class: {Name}";
|
||||
}
|
||||
}
|
||||
|
||||
public class FirstClassPassenger
|
||||
public class FirstClassPassenger : Passenger
|
||||
{
|
||||
public int AirMiles { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"First Class with {AirMiles:N0} air miles";
|
||||
return $"First Class with {AirMiles:N0} air miles: {Name}";
|
||||
}
|
||||
}
|
||||
|
||||
public class CoachClassPassenger
|
||||
public class CoachClassPassenger : Passenger
|
||||
{
|
||||
public double CarryOnKG { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Coach Class with {CarryOnKG:N2} KG carry on";
|
||||
return $"Coach Class with {CarryOnKG:N2} KG carry on: {Name}";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ public partial class Person
|
|||
if (married) return;
|
||||
spouse = partner;
|
||||
married = true;
|
||||
partner.Marry(this);
|
||||
partner.Marry(this); // this is the current object
|
||||
}
|
||||
|
||||
// static method to "multiply"
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ WriteLine($"{thing2.Name} has {thing2.Count} children.");
|
|||
WriteLine($"Deconstructed: {fruitName}, {fruitNumber}");
|
||||
|
||||
// Deconstructing a Person
|
||||
var (name1, dob1) = bob;
|
||||
var (name1, dob1) = bob; // implicitly calls the Deconstruct method
|
||||
WriteLine($"Deconstructed: {name1}, {dob1}");
|
||||
|
||||
var (name2, dob2, fav2) = bob;
|
||||
|
|
@ -227,12 +227,12 @@ WriteLine($"5! is {Person.Factorial(5)}");
|
|||
|
||||
// Pattern matching with objects
|
||||
|
||||
object[] passengers = {
|
||||
new FirstClassPassenger { AirMiles = 1_419 },
|
||||
new FirstClassPassenger { AirMiles = 16_562 },
|
||||
new BusinessClassPassenger(),
|
||||
new CoachClassPassenger { CarryOnKG = 25.7 },
|
||||
new CoachClassPassenger { CarryOnKG = 0 },
|
||||
Passenger[] passengers = {
|
||||
new FirstClassPassenger { AirMiles = 1_419, Name = "Suman" },
|
||||
new FirstClassPassenger { AirMiles = 16_562, Name = "Lucy" },
|
||||
new BusinessClassPassenger { Name = "Janice" },
|
||||
new CoachClassPassenger { CarryOnKG = 25.7, Name = "Dave" },
|
||||
new CoachClassPassenger { CarryOnKG = 0, Name = "Amit" },
|
||||
};
|
||||
|
||||
foreach (object passenger in passengers)
|
||||
|
|
|
|||
Loading…
Reference in a new issue