static void Main(string[] args) { var jeff = new ImmutablePerson { FirstName = "Jeff", LastName = "Winger" }; // jeff.FirstName = "Geoff"; var car = new ImmutableVehicle { Brand = "Mazda MX-5 RF", Color = "Soul Red Crystal Metallic", Wheels = 4 }; var repaintCar = car with { Color = "Polymetal Grey Metallic" }; WriteLine("Original color was {0}, new color is {1}.", arg0: car.Color, arg1: repaintCar.Color); var oscar = new ImmutableAnimal("Oscar", "Labrador"); var(who, what) = oscar; // calls deconstruct method WriteLine($"{who} is a {what}."); } void UsingPacktLib9() { object[] passengers = { new FirstClassPassenger { AirMiles = 1_419 }, new FirstClassPassenger { AirMiles = 16_562 },
private static void RunRecordsDemo() { // work with records var pavel = new ImmutablePerson { FirstName = "Pavel", LastName = "Talochka" }; //init exception, Has to intialize when object create // pavel.FirstName = "Michail"; var car = new ImmutableVehicle { Brand = "BMW", Color = "Soul Red Crystal Metallic", Wheels = 4 }; // cloning object var repaintingCar = car with { Color = "Pink" }; WriteLine("Original color of the car is {0}, new color is {1}", arg0: car.Color, arg1: repaintingCar.Color); // immutable animal example var oscar = new ImmutableAnimal("Oscar", "Labrador"); var(who, what) = oscar; WriteLine("{0} is a {1}.", arg0: who, arg1: what); }
static void Main(string[] args) { Person Olli = new("Olli", new DateTime(1983, 12, 17)); // ------------------------ page 180 - Record short version ------------------------ var kosto = new ImmutableAnimal("Kosto", "Cat"); var(name, species) = kosto; System.Console.WriteLine($"{name} is a {species}."); // ------------------------ page 179 - Init / Records ------------------------ var Lenchen = new ImmutablePerson { FirstName = "Magdalena", LastName = "Bergmann" }; var car = new ImmutableVehicle { Wheels = 4, Brand = "Ford", Color = "Blue", }; var repaintedCar = car with { Color = "Black" }; // repaintedCar.Color = "Green"; System.Console.WriteLine($"Original color was {car.Color}, repainted color is {repaintedCar.Color}."); // ------------------------ page 176 - Pattern Matching ------------------------ /* * object[] passengers = { * new FirstClassPassenger(){ AirMiles = 1_415}, * new FirstClassPassenger{ AirMiles = 16_562}, * new CoachClassPassenger{ CarryOnKG = 25.3}, * new CoachClassPassenger{ CarryOnKG = 0.3}, * new BusinessClassPassenger(), * }; * * foreach (object passenger in passengers) * { * decimal fligthCost = passenger switch * { * FirstClassPassenger p => p.AirMiles switch * { * > 35000 => 1500M, * > 15000 => 1750M, * _ => 2000M, * }, * * BusinessClassPassenger => 1000M, * * CoachClassPassenger p when p.CarryOnKG < 10.0 => 500M, * CoachClassPassenger => 650M, * _ => 800M, * }; * * System.Console.WriteLine($"Flight costs {fligthCost:C} for {passenger}"); * } * //*/ // ------------------------ page 172 - Properties and Indexers ------------------------ /* * System.Console.WriteLine(Olli.Origin); * System.Console.WriteLine(Olli.Age); * try * { * Olli.FavoritPrimaryColor = "yello"; * * } * catch (Exception e) * { * System.Console.WriteLine(e.Message); * } * System.Console.WriteLine(Olli.FavoritPrimaryColor); * * Olli.Children.Add(new Person("child1")); * Olli.Children.Add(new Person("child2")); * Olli.Children.Add(new Person("child3")); * System.Console.WriteLine(Olli.Children[4]); */ // ------------------------ page 167 - parameter passing ------------------------ /* * int a = 10; * int b = 20; * int c = 30; * System.Console.WriteLine($"Before: a = {a}, b = {b}, c = {c}"); * Olli.PassingParameters(a, ref b, out c); * System.Console.WriteLine($"After: a = {a}, b = {b}, c = {c}"); */ // ------------------------ page 165 - method overloading ------------------------ // System.Console.WriteLine(Olli.SayHello("Lena")); // ------------------------ page 162 - tuple / deconstruct tuple ------------------------ (string, int)fruits = Olli.GetFruit(); /* * System.Console.WriteLine($"There are {fruits.Item2} {fruits.Item1} "); * // Deconstruct tuple * (string fruit, int amount) = Olli.GetFruit(); * System.Console.WriteLine($"{fruit}, {amount}"); * * var fruitNamed = Olli.GetNamedFruit(); * System.Console.WriteLine($"There are {fruitNamed.Numbers} {fruitNamed.Name}"); */ // ------------------------ page 158 - default ------------------------ ThingOfDefaults defaultThing = new(); //System.Console.WriteLine(defaultThing); // ------------------------ page 157 - constructor ------------------------ Person Sigi = new("Sigi", new DateTime(2018, 09, 07)); //System.Console.WriteLine(Sigi.ToString()); // ------------------------ page 156 - const / read-only ------------------------ //System.Console.WriteLine($"{Olli.Name} is a {Person.Species}"); // ------------------------ page 155 - static ------------------------ BankAccount.InterestRate = 0.012M; var lenasAccount = new BankAccount(); lenasAccount.accountName = "Lenas Account"; lenasAccount.Balance = 5500; //System.Console.WriteLine(format: "{0} earned {1:C} interest.", arg0: lenasAccount.accountName, arg1: lenasAccount.Balance * BankAccount.InterestRate); // ------------------------ page 150 - basics ------------------------ Person Lena = new(); Lena.Name = "Lena"; Lena.favoriteWonder = WondersOfTheAncientWorld.LighthouseOfAlexandria; Lena.DateOfBirth = new DateTime(1986, 09, 07); Person Sigggi = new Person { Name = "Sigi", DateOfBirth = new DateTime(2019, 12, 24), favoriteWonder = (WondersOfTheAncientWorld).3 }; Lena.Children.Add(new Person { Name = "child1" }); Lena.Children.Add(new Person() { Name = "child2" }); /* * Console.WriteLine( * format: "{0} was born on {1:dddd, d MMMM yyyy} and likes {2} (int of wonder: ). ", * arg0: Lena.name, * arg1: Lena.dateOfBirth, * arg2: Lena.favoriteWonder); * * Console.WriteLine( * format: "{0} came to us on {1: dd MMM yy} and likes {2}", * arg0: Sigi.name, * arg1: Sigi.dateOfBirth, * arg2: Sigi.favoriteWonder); * * foreach (var child in Lena.children) { * System.Console.WriteLine(child.name); * } */ } }
static void Main(string[] args) { /*Person bob = new Person(); * bob.name = "Bob Smith"; * bob.DateOfBirth = new DateTime(1965, 12, 22); * bob.BucketList= WondersOfTheAncientWorld.Zeus|WondersOfTheAncientWorld.Mausoleum; * WriteLine( * format:"{0} was born on {1:dddd,d MMMM yyyy} and he is {2}", * arg0: bob.name, * arg1: bob.DateOfBirth, * arg2: Person.Species); * WriteLine($"{bob.name} lives on {bob.HomePlanet}"); * WriteLine( * format: "{0}'s bucket list is {1}", * arg0:bob.name, * arg1:bob.BucketList); * bob.Children.Add(new Person{name= "Alfred"}); * bob.Children.Add(new Person {name = "Zoe"}); * * WriteLine( * $"{bob.name} has {bob.Children.Count} children:"); * for (int child = 0; child <bob.Children.Count; child++) * { * WriteLine($" {bob.Children[child].name}"); * } * * var alice = new Person() * { * name = "Alice Jones", * DateOfBirth = new DateTime(1998, 3, 7) * }; * * WriteLine( * format: "{0} was born on {1:dddd MMMM yy}", * arg0: alice.name, * arg1: alice.DateOfBirth * ); * * BankAccount.InterestRate = 0.012M; //shared value * * var jonesAccount = new BankAccount(); * jonesAccount.AccountName = "Mrs. Jones"; * jonesAccount.Balance = 2400; * * WriteLine(format:"{0} earned {1:C} interest", * arg0:jonesAccount.AccountName, * arg1:jonesAccount.Balance * BankAccount.InterestRate); * * var blankPerson = new Person(); * WriteLine(format: * "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}", * arg0:blankPerson.name, * arg1:blankPerson.HomePlanet, * arg2:blankPerson.Instantiated); * * var gunny = new Person("Gunny", "Mars"); * WriteLine( * format:"{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}", * arg0:gunny.name, * arg1:gunny.HomePlanet, * arg2:gunny.Instantiated); * WriteLine($"Printing Methods for {bob.name}"); * bob.WriteToConsole(); * WriteLine(bob.GetOrigin()); * * var fruit = bob.GetFruit(); * WriteLine($"{fruit.Name}, {fruit.Number}"); * * WriteLine(bob.SayHelloTo("Emily")); * * int a = 10; * int b = 20; * int c = 30; * WriteLine($"before a= {a}, b = {b}, c = {c}"); * bob.PassingParameters(a,ref b, out c); * WriteLine($"after a = {a}, b = {b} c = {c}"); * * var sam = new Person * { * name = "Sam", * DateOfBirth = new DateTime(1972, 1, 27) * }; * /* WriteLine(sam.Origin); * WriteLine(sam.Greeting); * WriteLine(sam.Age);*/ /*sam.FavoriteIceCream = "Chocolate Fudge"; * WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}"); * sam.FavoritePrimaryColor = "Red"; * WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}"); */ /*sam.Children.Add(new Person {name="Charlie"}); * sam.Children.Add(new Person {name="Ella"}); * * WriteLine($"Sam's first child is {sam.Children[0].name}"); * WriteLine($"Sam's second child is {sam.Children[1].name}"); * * WriteLine($"Sam's second child is {sam[1].name}"); * WriteLine($"Sam's second child is {sam[1].name}"); */ /*object[] passengers = { * new FirstClassPassenger{AirMiles = 1_419}, * new FirstClassPassenger{AirMiles = 16_562}, * new BusinessClassPassenger(), * new CoachClassPassenger{CarryOnKG = 25.7}, * new CoachClassPassenger{CarryOnKG = 0}, * }; * * foreach(object passenger in passengers) * { * decimal flightCost = passenger switch * { * /* C8 syntax * FirstClassPassenger p when p.AirMiles > 35000 => 1500M, * * FirstClassPassenger p when p.AirMiles > 15000 => 1750M, * * FirstClassPassenger => 1500M, * BusinessClassPassenger => 2000M,*/ //c# 9 syntax /*FirstClassPassenger p => p.AirMiles switch * { * >3500 => 1500M, * >1500 => 1750M, * _ => 2000M * }, * BusinessClassPassenger => 1000M, * CoachClassPassenger p when p.CarryOnKG < 10.0 => 500M, * CoachClassPassenger => 650M, * _ =>800M * }; * WriteLine($"Flight costs {flightCost} for {passenger}"); * }*/ /* ImmutableVehicle car = new ImmutableVehicle * { * Brand = "Mazda MX-5 RF", * Color = "Metallic", * Wheels = 4 * }; * //creating new records from existing ones with any changed state 'non-destructive mutation' with keyword * var repaintedCar = car with {Color = "Polymetal Grey Metallic"}; * WriteLine("Original color was {0}, new color is {1}.", * arg0:car.Color, * arg1:repaintedCar.Color);*/ var oscar = new ImmutableAnimal("Oscar", "Labrador"); var(who, what) = oscar; //calls Deconstruct Method WriteLine($"{who} is {what}"); }