private static void DemoParameterPassing(Person p1) { // Exercise: Check how parameters are passed int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); p1.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); // simplified C# 7 syntax for out parameters int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); p1.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); }
static void Main(string[] args) { Person bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StetueOfZeusAtOlympia; bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // WriteLine(bob.ToString()); WriteLine(format: "{0} was born on {1:dddd, d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); WriteLine($"Bob is {bob.Age} years old."); WriteLine(format: "{0}'s favorite wonder is {1}. It's integer is {2}", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder); WriteLine($"{bob.Name}'s bucket list is {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}"); } 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}"); WriteLine("========================================================="); object[] passengers = { new FirstClassPassenger { AirMiles = 1_419 },
static void Main(string[] args) { BankAccount.InterestRate = 0.012M; #region Elena Instance var elena = new Person(); elena.Name = "Elena Ponce"; elena.DateOfBirth = new DateTime(2001, 3, 15); WriteLine($"{elena.Name} was born on {elena.DateOfBirth:dddd, MMMM d yyyy}"); elena.placesToVisit = PlacesToVisit.Budapest; elena.Children.Add(new Person { Name = "Dante" }); elena.Children.Add(new Person { Name = "Zoe" }); for (int children = 0; children < elena.Children.Count; children++) { WriteLine($"{elena.Children[children].Name}"); } WriteLine($"{elena.Name} is a {Person.Species} and was born on {elena.HomePlanet}"); #endregion #region Oscar Instance var oscar = new Person { Name = "Oscar Macias", DateOfBirth = new DateTime(2000, 10, 2) }; oscar.Children.Add(new Person { Name = "OscarJr" }); WriteLine($"{oscar.Name} was born on {oscar.DateOfBirth:dddd, MMMM d yyyy}"); #endregion #region Using BankAccount var adrianAccount = new BankAccount(); adrianAccount.AccountName = "El traidor"; adrianAccount.Balance = 2500; WriteLine($"{adrianAccount.AccountName} earned {adrianAccount.Balance * BankAccount.InterestRate:C}"); #endregion #region Using Constructor var blankPerson = new Person(); WriteLine($"{blankPerson.Name} of {blankPerson.HomePlanet} was created at {blankPerson.Instantiated:hh:mm:ss} on {blankPerson.Instantiated:dddd}"); var ricardo = new Person("Ricardo", "Mars"); WriteLine($"{ricardo.Name} of {ricardo.HomePlanet} was created at {blankPerson.Instantiated:hh:mm:ss} on {ricardo.Instantiated:dddd}"); var defaultGuy = new DefaultThings(); WriteLine($"{defaultGuy.Name} , {defaultGuy.People}, {defaultGuy.Population}, {defaultGuy.When}"); #endregion #region Using tuple Method (string, int)fruit = oscar.GetNameFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} eats"); var elenaFruit = elena.GetNameFruit(); WriteLine($"{elenaFruit.Name}, {elenaFruit.Number} eats"); WriteLine(elena.SayHello()); WriteLine(oscar.Greeting()); #endregion #region Ref and Out int a = 10; int b = 20; int c = 30; WriteLine($"Before : a = {a}, b = {b}, c = {c}"); elena.PassingParameters(a, ref b, out c); WriteLine($"After : a = {a}, b = {b}, c = {c}"); #endregion #region using Properties var adr = new Person { Name = "El traidor", DateOfBirth = new DateTime(2001, 11, 12) }; WriteLine(adr.Greeting()); WriteLine(adr.Age); adr.FavoriteIceCream = "Chocolate"; WriteLine(adr.FavoriteIceCream); adr.FavoriteColor = "RED"; WriteLine($"Adr favorite color is {adr.FavoriteColor}"); #endregion #region Using index WriteLine($"Elena's first born is {elena.Children[0].Name}"); #endregion #region Making ana angry (Using delegates) Person ana = new Person(); ana.Shout = Ana_Shout; ana.Shout += Elena_Shout; ana.Shout += Adrian_Shout; // ana.Poke(); // ana.Poke(); // ana.Poke(); // ana.Poke(); #endregion #region Using ComparableInterface Person [] people = { new Person { Name = "Ana" }, new Person { Name = "Tiesel" }, new Person { Name = "Kaleb" }, new Person { Name = "Ricardo" } }; WriteLine("List of people: "); foreach (var person in people) { WriteLine(person.Name); } WriteLine("Using IComparable sort to sort people"); Array.Sort(people); foreach (var person in people) { WriteLine(person.Name); } WriteLine("Using IComparer sort to sort people"); Array.Sort(people, new PersonComprarer()); foreach (var person in people) { WriteLine(person.Name); } #endregion }
static void Main(string[] args) { var p1 = new Person(); p1.Name = "Bob Smith"; p1.DateOfBirth = new System.DateTime(1965, 12, 22); WriteLine($"{p1.Name} was born on {p1.DateOfBirth:dddd, d MMMM yyyy}"); var p2 = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 17) }; WriteLine($"{p2.Name} was born on {p2.DateOfBirth:d MMMM yy}"); p1.FavouriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine($"{p1.Name}'s favourite wonder is {p1.FavouriteAncientWonder}"); p1.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // p1.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{p1.Name}'s bucket list is {p1.BucketList}"); p1.Children.Add(new Person { Name = "Alfred" }); p1.Children.Add(new Person { Name = "Zoe" }); WriteLine($" {p1.Name} has {p1.Children.Count} children:"); for (int child = 0; child < p1.Children.Count; child++) { WriteLine($" {p1.Children[child].Name}"); } BankAccount.InterestRate = 0.012M; var ba1 = new BankAccount(); ba1.AccountName = "Mrs. Jones"; ba1.Balance = 2400; WriteLine($"{ba1.AccountName} earned {ba1.Balance * BankAccount.InterestRate:C} interest."); var ba2 = new BankAccount(); ba2.AccountName = "Ms. Gerrier"; ba2.Balance = 98; WriteLine($"{ba2.AccountName} earned {ba2.Balance * BankAccount.InterestRate:C} interest."); WriteLine($"{p1.Name} is a {Person.Species}"); var p3 = new Person(); WriteLine($"{p3.Name} was instantiated at {p3.Instantiated:hh:mm:ss} on {p3.Instantiated:dddd, d MMMM yyyy}"); var p4 = new Person("Aziz"); WriteLine($"{p4.Name} was instantiated at { p4.Instantiated:hh:mm:ss} on { p4.Instantiated:dddd, d MMMM yyyy}"); p1.WriteToConsole(); WriteLine(p1.GetOrigin()); Tuple <string, int> fruit4 = p1.GetFruitCS4(); WriteLine($"There are {fruit4.Item2} {fruit4.Item1}."); (string, int)fruit7 = p1.GetFruitCS7(); WriteLine($"{fruit7.Item1}, {fruit7.Item2} there are."); var fruitNamed = p1.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} {fruitNamed.Name}?"); // C# 7.0 var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (p1.Name, p1.Children.Count); WriteLine($"{thing2.Item1} has {thing2.Item2} children."); // C# 7.1 var thing3 = (p1.Name, p1.Children.Count); WriteLine($"{thing3.Name} has {thing3.Count} children."); // деконструкция кортежей (string fruitName, int fruitNumber) = p1.GetFruitCS7(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(p1.SayHello()); WriteLine(p1.SayHello("Emily")); WriteLine(p1.OptionalParameters()); WriteLine(p1.OptionalParameters("Jump!", 98.5)); WriteLine(p1.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(p1.OptionalParameters("Poke!", active: false)); int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); p1.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); // упрощенный синтаксис параметров out в C# 7 int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); p1.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); 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 first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); WriteLine(format: "{0} was born on {1:dddd,d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); // var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine(format: "{0} was born on {1:dd MMM yy}", arg0: alice.Name, arg1: alice.DateOfBirth); bob.FavouriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine(format: "{0}'s favourite wonder is {1}. Its interger is {2}.", arg0: bob.Name, arg1: bob.FavouriteAncientWonder, arg2: (int)bob.FavouriteAncientWonder); //bob.BucketList = WondersOfTheAncientWorld.MausoleumAtHalicarnassus | WondersOfTheAncientWorld.HangingGardensOfBabylon; bob.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{bob.Name}'s bucket list is {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}"); } //Making a field static BankAccount.InterestRate = 0.012M; //store a 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 ); //Making a field constant WriteLine($"{bob.Name} is a {Person.Species} and PI constant is {Math.PI}."); //Making a field Read-only WriteLine($"{bob.Name} was born on {bob.HomePlanet}."); //Initializing fields with constructors 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); //Returnig values from methods bob.WriteToConsole(); WriteLine(bob.GetOrigin()); //call tuple method (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); //Naming fields of a tuple var fruitNamed = bob.GetFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}"); //infering tuple names var things1 = ("Neville", 4); WriteLine($"{things1.Item1} has {things1.Item2} children"); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); //Deconstructin tuples // deconstruct return value into two separate variables (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); // fruitName // fruitNumber //Defining and Passing Parameters to methods WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); //Passing Optional Parameters & Naming Arguments WriteLine(bob.OptionalParameters(number: 98.33, command: "Alright!")); //Controlling how parameters are passed 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}"); int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); //simplified C# 7.0 syntax for out parameter bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); //Access Control with Properties & Indexers var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); //Defining Settable Properties sam.FavouriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favourite ice-cream floavor is {sam.FavouriteIceCream}."); sam.FavouritePrimaryColor = "GREen"; WriteLine($"Sam's favourite primary color is {sam.FavouritePrimaryColor}"); //Defining Indexers 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}"); //Pattern matching with Objects object[] passengers = { new FirstClassPassenger { AirMiles = 1_419 },
static void Main(string[] args) { // Setting and outputting field values var bob = new Person(); WriteLine(bob.ToString()); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); WriteLine( format: "{0} was born on {1:dddd, d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine( format: "{0} was born on {1:dd MMM yy}", arg0: alice.Name, arg1: alice.DateOfBirth); // Storing a value using an enum type bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine(format: "{0}'s favorite wonder is {1}. It's integer is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder); // Storing multiple values using an enum type bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus | WondersOfTheAncientWorld.StatueOfZeusAtOlympia; // bob.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{bob.Name}'s bucket list is {bob.BucketList}"); // Storing multiple values using collections 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}"); } foreach (var chi in bob.Children) { WriteLine($"=>{chi.Name}"); } // Making a field static BankAccount.InterestRate = 0.012M; // store a 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 gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; WriteLine(format: "{0} earned {1:C} interest.", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance * BankAccount.InterestRate); BankAccount.InterestRate = 0.02M; WriteLine(format: "{0} earned {1:C} interest.(InterestRate changed)", arg0: jonesAccount.AccountName, arg1: jonesAccount.Balance * BankAccount.InterestRate); WriteLine(format: "{0} earned {1:C} interest.(InterestRate changed)", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance * BankAccount.InterestRate); // Making a field constant WriteLine($"{bob.Name} is a {Person.Species}"); // Making a field read-only WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); // Initializing fields with constructors 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); // Returning values from methods bob.WriteToConsole(); WriteLine(bob.GetOrigin()); // Combining multiple returned values using tuples (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); // Naming the fields of a tuple var fruitNamed = bob.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}."); // Inferring tuple names var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); // Deconstructing tuples (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); // Defining and passing parameters to methods WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); // Passing optional parameters and naming arguments WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); WriteLine(bob.OptionalParameters( number: 52.7, command: "Hide!")); WriteLine(bob.OptionalParameters("Poke!", active: false)); // Controlling how parameters are passed 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}"); int d = 10; int e = 20; WriteLine( $"Before: d = {d}, e = {e}, f doesn't exist yet!"); // simplified C# 7 syntax for the out parameter bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); // Defining read-only properties var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); // Defining settable properties 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}."); // Defining indexers 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 first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { // bob var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1970, 11, 30); bob.FavouriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; bob.Children.Add(new Person { Name = "Alf" }); bob.Children.Add(new Person { Name = "Zoe" }); WriteLine(); WriteLine("Bob-----------------------------------------"); WriteLine(bob.getOrigin()); bob.WriteToConsole(); WriteLine( format: "{0} was born on {1:dddd, d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth ); WriteLine( format: "{0}'s favourite wonder is {1}, it's integer is {2}", arg0: bob.Name, arg1: bob.FavouriteAncientWonder, arg2: (int)bob.FavouriteAncientWonder ); WriteLine($"{bob.Name}'s bucket list is: {bob.BucketList}"); WriteLine($"{bob.Name} has {bob.Children.Count} children"); foreach (Person child in bob.Children) { WriteLine($"child name: {child.Name}"); } WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} is from {bob.HomePlanet}"); WriteLine(); WriteLine("Tuples-----------------------------------------"); (string, int)fruit = bob.GetFruit(); WriteLine($"USING TUPLES: {fruit.Item1}, {fruit.Item2} there are."); var vegetablesNamed = bob.GetVeg(); WriteLine($"USING named TUPLES: {vegetablesNamed.Name}, {vegetablesNamed.Number} there are."); // Deconstructing tuples (string name, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed TUPLE: {name}, {fruitNumber}"); // Inferring tuple names var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Item1} has {thing2.Item2} children-"); // optional parameters WriteLine(); WriteLine("Optional Parameters------------------------------"); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump", 2.4)); WriteLine(bob.OptionalParameters(number: 2.4, active: false)); WriteLine(bob.OptionalParameters(active: false, command: "Fly")); WriteLine(bob.OptionalParameters("Walk", active: false)); // alice var alice = new Person(); alice.Name = "Alice Smith"; alice.DateOfBirth = new DateTime(1968, 10, 22); WriteLine(); WriteLine("Alice------------------------------"); WriteLine( format: "{0} was born on {1:dd, MMM yy}", arg0: alice.Name, arg1: alice.DateOfBirth ); WriteLine(); WriteLine("Constructors----------------------------"); // blank Person to use constructor 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 ); // bank account class use BankAccount.InterestRate = 0.012M; var jonesAccount = new BankAccount(); jonesAccount.AccountName = "Mrs Jones"; jonesAccount.Balance = 2400; var smithAccount = new BankAccount(); smithAccount.AccountName = "Mrs Smith"; smithAccount.Balance = 800; WriteLine(); WriteLine("public STATIC property - shared across all instances of class - bank interest rate demonstrateS ---------------"); WriteLine( format: "{0} earned {1:C} interest", arg0: jonesAccount, arg1: jonesAccount.Balance * BankAccount.InterestRate ); WriteLine( format: "{0} earned {1:C} interest", arg0: smithAccount, arg1: smithAccount.Balance * BankAccount.InterestRate ); WriteLine(); WriteLine("PARAMETERS - 3 ways: by value, reference or an an out parameter ---------------"); int a = 10; int b = 20; int c = 30; // no point in setting value of c as gets repoaced from within the method bob.PassingParameters(a, ref b, out c); WriteLine($"a: {a}, b: {b}, c {c}"); int d = 10; int e = 20; WriteLine($"d: {d}, e: {e}, f doesn't exist yet"); bob.PassingParameters(d, ref e, out int f); WriteLine($"d: {d}, e: {e}, f: {f}"); WriteLine(); WriteLine("Partial classes and properties, and indexers ---------------"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavouriteIceCream = "Mint choc chip"; sam.FavouritePrimaryColour = "Red"; WriteLine($"{sam.Name}'s favourite ice cream is {sam.FavouriteIceCream} and fav primary colour is {sam.FavouritePrimaryColour}"); sam.Children.Add(new Person { Name = "Charlie" }); sam.Children.Add(new Person { Name = "Ella" }); // if you had no indexer set up for Children: WriteLine($"{sam.Name}'s first child is {sam.Children[0].Name}"); WriteLine($"{sam.Name}'s second- child is {sam.Children[1].Name}"); // if you have indexer set up for Children - does it really add much value? I think it reduces ease of reading code... WriteLine($"{sam.Name}'s first child is {sam[0].Name}"); WriteLine($"{sam.Name}'s second- child is {sam[1].Name}"); }
static void Main(string[] args) { var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; bob.BuckedList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Zoe" }); WriteLine(format: "{0} was born on {1:dddd, d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); WriteLine(format: "{0}'s favorite wonder is {1}. Its integer is {2}", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder); WriteLine($"{bob.Name}'s bucket list is {bob.BuckedList}"); WriteLine($"{bob.Name} has {bob.Children.Count} children:"); for (int child = 0; child < bob.Children.Count; child++) { WriteLine($" {bob.Children[child].Name}"); } BankAccount.InterestRate = 0.012M; 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 gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Mrs. Gerrier"; gerrierAccount.Balance = 98; WriteLine(format: "{0} earned {1:C} interest", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance * BankAccount.InterestRate); WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); 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); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = bob.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}"); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children"); (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); WriteLine(bob.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(bob.OptionalParameters("Poke!", active: false)); int a = 10; int b = 20; int c = 30; WriteLine($"Befire 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.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 first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); object[] passengers = { new FirstClassPassenger { AirMiles = 1_419 },
static void Main(string[] args) { var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Zoe" }); bob.WriteToConsole(); var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; alice.WriteToConsole(); BankAccount.InterestRate = 0.012M; // store a shared value var jonesAccount = new BankAccount(); jonesAccount.AccountName = "Mrs. Jones"; jonesAccount.Balance = 2400; var gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Mr. Gerrier"; gerrierAccount.Balance = 98; var blankPerson = new Person(); WriteLine( format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}", blankPerson.Name, blankPerson.HomePlanet, blankPerson.Instantiated ); var gunny = new Person("Gunny", "Mars"); WriteLine( format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", gunny.Name, gunny.HomePlanet, gunny.Instantiated ); var fruit = bob.GetFruit(); WriteLine($"{fruit.Name}, {fruit.Number} there are."); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1}, {thing1.Item2} there are."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name}, {thing2.Count} there are."); // // store return value in a typle variable with two fields // (string name, int age) tupleWithNamedFields = GetPerson(); // // typleWithNamedFields.name // // typleWithNamedFields.age // // deconstruct return value into two separate variables // (string name, int age) = GetPerson(); // // name // // age (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"deconstructed: {fruitName}, {fruitNumber}"); // command is Run!, number is 0, active is True WriteLine(bob.OptionalParameters()); // command is jump!, number is 98.5, active is True WriteLine(bob.OptionalParameters("Jump!", 98.5)); // command is Hide!, number is 52.7, active is True WriteLine(bob.OptionalParameters(number: 52.7, command: "Hide!")); 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}."); // indexers 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}"); }
static void Main(string[] args) { var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; WriteLine( format: "{0} was born on {1:dddd, d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); /* * WriteLine(format: * "{0}'s favorite wonder is {1}. It's integer is {2}.", * arg0: bob.Name, * arg1: bob.FavoriteAncientWonder, * arg2: (int)bob.FavoriteAncientWonder); */ //Bob Smith's bucket list is HangingGardensOfBabylon, MausoleumAtHalicarnassus WriteLine($"{bob.Name}'s bucket list is {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}"); } BankAccount.InterestRate = 0.012M; // store a 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 gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; WriteLine(format: "{0} earned {1:C} interest.", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance * BankAccount.InterestRate); WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); 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); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = bob.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}."); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); // store return value in a tuple variable with two fields // (string name, int age) tupleWithNamedFields = GetPerson(); // tupleWithNamedFields.name // tupleWithNamedFields.age // deconstruct return value into two separate variables // (string name, int age) = GetPerson(); // name // age (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); WriteLine(bob.OptionalParameters( number: 52.7, command: "Hide!")); WriteLine(bob.OptionalParameters("Poke!", active: false)); 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}"); int d = 10; int e = 20; WriteLine( $"Before: d = {d}, e = {e}, f doesn't exist yet!"); // simplified C# 7.0 syntax for the out parameter bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); 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 first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { //声明变量,初始化变化,输出变量信息。 var bob = new Person(); bob.Name = "Bob smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); WriteLine(format: "{0} was born on {1: dddd, d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); //新增bob变量的FavoriteAncientWonder的值。 bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Zoe" }); WriteLine($"{bob.Name} has {bob.Children.Count} children:"); foreach (Person p1 in bob.Children) { WriteLine($"{p1.Name}"); } // for (int child = 0; child < bob.Children.Count; child++) // { // WriteLine($" {bob.Children[child].Name}") ; // } //bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; WriteLine($"{bob. Name}' s bucket list is {bob. BucketList}"); //输出 WriteLine(format: "{0}'s FavoriteAncientWonder is {1},It's integer is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)WondersOfTheAncientWorld.StatueOfZeusAtOlympia); //新增alice变量 var alice = new Person() { Name = "Alice Jones", DateOfBirth = new DateTime(1984, 7, 3) }; WriteLine(format: "{0} was born on {1:dd MMM yy}", arg0: alice.Name, arg1: alice.DateOfBirth); BankAccount.InterestRate = 0.012M; var jonesAccount = new BankAccount(); jonesAccount.AccountName = "Mrs jones"; jonesAccount.Balance = 2400M; WriteLine(format: "{0} earned {1:C} interest rate.", arg0: jonesAccount.AccountName, arg1: jonesAccount.Balance * BankAccount.InterestRate); var gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Mr gerrier"; gerrierAccount.Balance = 98M; WriteLine(format: "{0} earned {1:C} interest rate.", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance * BankAccount.InterestRate); WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} was born in {bob.HomePlanet}"); var blankPerson = new Person(); WriteLine(format: "{0} of {1} was create 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 create at {2:hh:mm:ss} on a {2:dddd}.", arg0: gunny.Name, arg1: gunny.HomePlanet, arg2: gunny.Instantiated); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); (string Name, int Number)fruit = bob.GetFruit(); (string Name, int Number) = bob.GetFruit(); WriteLine($"There are {fruit.Number} {fruit.Name}."); WriteLine($"Deconstructed: {Name},{Number}"); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children. "); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children. "); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("jump", 98.5)); WriteLine(bob.OptionalParameters(number: 52.9, command: "Hide!")); WriteLine(bob.OptionalParameters("Poke!", active: true)); int a = 10, b = 20, c = 30; WriteLine("a = {0},b = {1},c = {2},", a, b, c); bob.PassingParameters(a, ref b, out c); WriteLine("a = {0},b = {1},c = {2},", a, b, c); int d = 10, e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn' t exist yet! "); // simplified C# 7. 0 syntax for the out parameter bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); 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}. "); test t1 = new test(); t1.setProperty("test"); WriteLine(t1.getTest()); //Indexers 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 first child is {sam[0].Name}"); WriteLine($"Sam' s second child is {sam[1].Name}"); }
static void Chapter05() { var p1 = new Person(); p1.Name = "Bob Smith"; p1.DateOfBirth = new DateTime(1965, 12, 22); WriteLine($"{p1.Name} was born on {p1.DateOfBirth:dddd, d MMMM yyyy}"); var p2 = new Person() { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 17) }; WriteLine($"{p2.Name} was born on {p2.DateOfBirth:d MMM yy}"); // without flags - one only p1.FavouriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine($"{p1.Name}'s favourite wonder is {p1.FavouriteAncientWonder}"); // p1.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | // WondersOfTheAncientWorld.MausoleumAtHalicarnassus; p1.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{p1.Name}'s bucket list is {p1.BucketList}"); // Add people to chlidren list p1.Children.Add(new Person("Alfred")); p1.Children.Add(new Person("Zoe")); WriteLine($"{p1.Name} has {p1.Children.Count} children:"); for (int child = 0; child < p1.Children.Count; child++) { WriteLine($" {p1.Children[child].Name}"); } BankAccount.InterestRate = 0.012M; var ba1 = new BankAccount(); ba1.AccountName = "Mrs. Jones"; ba1.Balance = 2400; WriteLine($"{ba1.AccountName} earned {ba1.Balance * BankAccount.InterestRate:C} Interest."); var ba2 = new BankAccount { AccountName = "Ms. Gerrier", Balance = 98 }; WriteLine($"{ba2.AccountName} earned {ba2.Balance * BankAccount.InterestRate:C} Interest."); WriteLine($"{p1.Name} is a {Person.Species}"); var p3 = new Person(); WriteLine($"{p3.Name} was instantiated at {p3.Instantiated:hh:mm:ss} on {p3.Instantiated:dddd, d MMMM yyyy}"); var p4 = new Person("Aziz"); WriteLine($"{p4.Name} was instantiated at {p4.Instantiated:hh:mm:ss} on {p4.Instantiated:dddd, d MMMM yyyy}"); p1.WriteToConsole(); WriteLine(p1.GetOrigin()); Tuple <string, int> fruit4 = p1.GetFruitCS4(); WriteLine($"There are {fruit4.Item2} {fruit4.Item1}."); (string, int)fruit7 = p1.GetFruitCS7(); WriteLine($"{fruit7.Item1}, {fruit7.Item2} there are."); var fruitNamed = p1.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} {fruitNamed.Name}?"); // Inferring tuple names var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children"); var thing2 = (p1.Name, p1.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children"); // YOu can deconstruct Tuples (string fruitName, int fruitNumber) = p1.GetFruitCS7(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); // say hello WriteLine(p1.SayHello()); WriteLine(p1.SayHello("Emily")); // optional parameters WriteLine(p1.OptionalParameters()); // pass parameters in order WriteLine(p1.OptionalParameters("Jump!", 98.5)); // use named parameters to negate requirement for order WriteLine(p1.OptionalParameters(number: 52.7, command: "Hide!")); // skip paramters using named arguments WriteLine(p1.OptionalParameters(command: "Poke!", active: false)); // passing parameters int a = 10; int b = 20; // value a is recreated in method and not returned. // reference to b is passed - which is why it can change // c is created in the method and so anything passed in is lost. WriteLine($"Before: a = {a}, b = {b}, c isn't created yet!"); p1.PassingParameters(a, ref b, out int c); WriteLine($"Before: a = {a}, b = {b}, c = {c}"); // Using partial Person class var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); // using getters and setters sam.FavouriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favourite ice-cream flavour is {sam.FavouriteIceCream}."); sam.FavouritePrimaryColor = "Red"; WriteLine($"Sam's favourite primary color is {sam.FavouritePrimaryColor}."); try { sam.FavouritePrimaryColor = "Yellow"; WriteLine($"Sam's favourite primary color is {sam.FavouritePrimaryColor}."); } catch (Exception ex) { WriteLine($"{ex.GetType()}: {ex.Message}"); } }
static void Main(string[] args) { Person bob = new Person(); WriteLine(bob.ToString()); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 2); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine( format: "{0} was born on {1:dddd, dd/MM/yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); WriteLine(format: "{0}'s favorite wonder is {1}. Its integer is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder); bob.Children.Add(new Person()); 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}"); } WriteLine($"{bob.Name}'s 1st child is {bob.Children[0].Name}"); // indexer WriteLine($"{bob.Name}'s 1st child is {bob[0].Name}"); // const field WriteLine( $"{bob.Name} is a {Person.Species}"); // Add Friend bob.addFriend(new Person("Edward", "Earth")); bob.addFriend(new Person("Edwin", "Mars")); List<Person> allFriends = bob.getFriend(); for(int idx=0; idx< allFriends.Count; idx++){ WriteLine($"{bob.Name}'s friend {idx}: {allFriends[idx].Name}"); } for(int idx=0; idx< allFriends.Count; idx++){ string idxString = idx.ToString(); WriteLine($"{bob.Name}'s friend {idx}: {bob[idxString].Name}"); } // calling Method WriteLine("** WriteLine(bob.GetOrigin())"); WriteLine(bob.GetOrigin()); WriteLine("** bob.WriteToConsole()"); bob.WriteToConsole(); var fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = bob.GetNamedFruit(); WriteLine($"{fruitNamed.Name}, {fruitNamed.Number} there are."); (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); (string fruitName, int fruitNumber) dcFruit = bob.GetFruit(); WriteLine($"Deconstructed 2: {dcFruit.fruitName}, {dcFruit.fruitNumber}"); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Edward")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); WriteLine(bob.OptionalParameters(command: "ALL!", active: false)); int d = 10; int e = 20; WriteLine( $"Before: d = {d}, e = {e}, f doesn't exist yet!"); // simplified C# 7.0 syntax for the out parameter bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine( format: "{0} was born on {1:dddd, dd/MM/yyyy}", arg0: alice.Name, arg1: alice.DateOfBirth); 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(initialName:"Gunny", homePlanet:"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); // Things of Default var default1 = new ThingOfDefaults(); WriteLine("** Thing of Default"); WriteLine( format: "int: {0}, DateTime:{1}, string:{2}, {3},", default1.Population, default1.When, default1.Name, default1.People.Count ); // ---------------------- // BankAccount // ---------------------- BankAccount.InterestRate = 0.012M; // store a shared value var jonesAccount = new BankAccount { AccountName= "Mrs. Jones", Balance= 2400 }; WriteLine(format: "{0}'s Balanace is {1:N} earned {2:C} interest.", arg0: jonesAccount.AccountName, arg1: jonesAccount.Balance, arg2: jonesAccount.Balance * BankAccount.InterestRate); var gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; WriteLine(format: "{0}'s Balanace is {1:N} earned {2:C} interest.", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance, arg2: gerrierAccount.Balance * BankAccount.InterestRate); // Try Property var sam = new Person(initialName:"Sam", homePlanet:"Mars", initBirth: new DateTime(1972, 1, 27)); WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavoriteIceCream = "Stars"; WriteLine($"{sam.Name}'s favorite icecream is {sam.FavoriteIceCream}"); sam.FavoritePrimaryColor="red"; WriteLine($"{sam.Name}'s favorite Color is {sam.FavoritePrimaryColor}"); try{ sam.FavoritePrimaryColor="yellow"; } catch (ArgumentException argEx){ WriteLine($"{argEx.GetType()} says {argEx.Message}"); } // -------------------------------------- // Flight Patterns(object pattern matching) // -------------------------------------- WriteLine("// -------------------------------"); WriteLine("//Flight Patterns(object pattern matching"); WriteLine("// -------------------------------"); var passengers = new object[5]{ new FirstClassPassenger { AirMiles = 1_419 },
static void Main(string[] args) { var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); WriteLine( format: "{0} was born on {1:dddd, d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); // WriteLine($"{bob.Name} was born on {bob.DateOfBirth:dddd, d MMMM yyyy}"); var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine( format: "{0} was born on {1:dd MMM y}", arg0: alice.Name, arg1: alice.DateOfBirth); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine( format: "{0}'s favorite wonder is {1}. Its integer is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder); bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // other way to write this is below--don't use it b/c it's harder to read // bob.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{bob.Name}'s bucket list is {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}"); } // foreach (Person child in bob.Children) // { // // WriteLine($" {child.Name}"); // Write($" {child.Name} "); // } // WriteLine(); BankAccount.InterestRate = 0.012M; // store a 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 gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; WriteLine(format: "{0} earned {1:C} interest.", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance * BankAccount.InterestRate); WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); 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); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = bob.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}."); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); WriteLine(bob.OptionalParameters( number: 52.7, command: "Hide!")); WriteLine(bob.OptionalParameters("Poke!", active: false)); 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}"); int d = 10; int e = 20; WriteLine( $"Before: d = {d}, e = {e}, f doesn't exist yet!"); //simplified C# 7.0 syntax for the out parameter bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); 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 first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); object[] passengers = { new FirstClassPassenger { AirMiles = 1_419 },
static void Main(string[] args) { var p1 = new Person(); p1.Name = "Bob Smith"; p1.DateOfBirth = new System.DateTime(1965, 12, 22); WriteLine($"{p1.Name} was born on {p1.DateOfBirth:dddd, d MMMM yyyy}"); var p2 = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 17) }; WriteLine($"{p2.Name} was born on {p2.DateOfBirth:d MMM yy}"); p1.BucketList = WondersOfTheAncientWorld.HangingGardenOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // p1.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{p1.Name}'s bucket list is {p1.BucketList}"); p1.Children.Add(new Person { Name = "Alfred" }); p1.Children.Add(new Person { Name = "Zoe" }); WriteLine( $"{p1.Name} has {p1.Children.Count} children:" ); for (int child = 0; child < p1.Children.Count; child++) { WriteLine($" {p1.Children[child].Name}"); } BankAccount.InterestRate = 0.012M; var ba1 = new BankAccount(); ba1.AccountName = "Mrs. Jones"; ba1.Balance = 2400; WriteLine($"{ba1.AccountName} earned {ba1.Balance * BankAccount.InterestRate:C} interest."); var ba2 = new BankAccount(); ba2.AccountName = "Ms. Gerrier"; ba2.Balance = 98; WriteLine($"{ba2.AccountName} earned {ba2.Balance * BankAccount.InterestRate:C} interest."); WriteLine($"{p1.Name} is a {Person.Species}"); // WriteLine($"{p1.Name} is a {p1.Species}"); WriteLine($"{p1.Name} was born on {p1.HomePlanet}"); var p3 = new Person(); WriteLine($"{p3.Name} was instantiated at {p3.Instantiated:hh:mm:ss} on {p3.Instantiated:dddd, d MMMM yyyy}"); var p4 = new Person("Aziz"); WriteLine($"{p4.Name} was instantiated at {p4.Instantiated:hh:mm:ss} on {p4.Instantiated:dddd, d MMMM yyyy}"); p1.WriteToConsole(); WriteLine(p1.GetOrigin()); Tuple <string, int> fruit4 = p1.GetFruitCS4(); WriteLine($"There are {fruit4.Item2} {fruit4.Item1}."); (string, int)fruit7 = p1.GetFruitCS7(); WriteLine($"{fruit7.Item1}, {fruit7.Item2} there are."); var fruitNamed = p1.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} {fruitNamed.Name}?"); var thing1 = ("Neville", 4); WriteLine( $"{thing1.Item1} has {thing1.Item2} children." ); var thing2 = (p1.Name, p1.Children.Count); WriteLine( $"{thing2.Name} has {thing2.Count} children." ); (string fruitName, int fruitNumber) = p1.GetFruitCS7(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(p1.SayHello()); WriteLine(p1.SayHello("Emily")); WriteLine(p1.OptionalParameters()); WriteLine(p1.OptionalParameters("Jump!", 98.5)); WriteLine(p1.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(p1.OptionalParameters("Poke!", active: false)); int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); p1.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); // simplified C# 7 syntax for out parameters int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); p1.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); 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 first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); var harry = new Person { Name = "Harry" }; var mary = new Person { Name = "Mary" }; var jill = new Person { Name = "Jill" }; // call instance method var baby1 = mary.ProcreateWith(harry); // call static method var baby2 = Person.Procreate(harry, jill); // call an operator var baby3 = harry * mary; WriteLine($"{mary.Name} has {mary.Children.Count} children."); WriteLine($"{harry.Name} has {harry.Children.Count} children."); WriteLine($"{jill.Name} has {jill.Children.Count} children."); WriteLine($"{mary.Name}'s first child is named \"{mary.Children[0].Name}\"."); WriteLine($"5! is {Person.Factorial(5)}"); harry.Shout += Harry_Shout; harry.Poke(); harry.Poke(); harry.Poke(); harry.Poke(); Person[] people = { new Person { Name = "Simon" }, new Person { Name = "Jenny" }, new Person { Name = "Adam" }, new Person { Name = "Richard" } }; WriteLine("Initial list of people:"); foreach (var person in people) { WriteLine($"{person.Name}"); } WriteLine("Use Person's IComparable implementation to sort:"); Array.Sort(people); foreach (var person in people) { WriteLine($"{person.Name}"); } WriteLine("use PersonComparer's IComparer implementation to sort:"); Array.Sort(people, new PersonComparer()); foreach (var person in people) { WriteLine($"{person.Name}"); } var t = new Thing(); t.Data = 42; WriteLine($"Thing: {t.Process("42")}"); var gt = new GenericThing <int>(); gt.Data = 42; WriteLine($"GenericThing: {gt.Process("42")}"); string number1 = "4"; WriteLine($"{number1} squared is {Squarer.Square<string>(number1)}"); byte number2 = 3; WriteLine($"{number2} squared is {Squarer.Square<byte>(number2)}"); var dv1 = new DisplacementVector(3, 5); var dv2 = new DisplacementVector(-2, 7); var dv3 = dv1 + dv2; WriteLine($"({dv1.X}, {dv1.Y}) + ({dv2.X}, {dv2.Y}) = ({dv3.X}, {dv3.Y})"); Employee e1 = new Employee { Name = "John Jones", DateOfBirth = new DateTime(1990, 7, 28) }; e1.WriteToConsole(); e1.EmployeeCode = "JJ001"; e1.HireDate = new DateTime(2014, 11, 23); WriteLine($"{e1.Name} was hired on {e1.HireDate:dd/MM/yy}"); WriteLine(e1.ToString()); Employee aliceInEmployee = new Employee { Name = "Alice", EmployeeCode = "AA123" }; Person aliceInPerson = aliceInEmployee; aliceInEmployee.WriteToConsole(); aliceInPerson.WriteToConsole(); WriteLine(aliceInEmployee.ToString()); WriteLine(aliceInPerson.ToString()); if (aliceInPerson is Employee) { WriteLine($"{nameof(aliceInPerson)} IS an Employee"); Employee e2 = (Employee)aliceInPerson; // do something with e2 } Employee e3 = aliceInPerson as Employee; if (e3 != null) { WriteLine($"{nameof(aliceInPerson)} AS an Employee"); // do something with e3 } try { e1.TimeTravel(new DateTime(1999, 12, 31)); e1.TimeTravel(new DateTime(1950, 12, 25)); } catch (PersonException ex) { WriteLine(ex.Message); } string email1 = "*****@*****.**"; string email2 = "ian&test.com"; WriteLine($"{email1} is a valid e-mail address: {email1.isValidEmail()}."); WriteLine($"{email2} is a valid e-mail address: {email2.isValidEmail()}."); }
static void Main(string[] args) { Person bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 2); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; WriteLine(format: "{0} was born on {1:dddd, dd MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); WriteLine(format: "{0}'s favorite wonder is {1}. Its byte is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (byte)bob.FavoriteAncientWonder); bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Zoe" }); WriteLine($"{bob.Name} has {bob.Children.Count} children:"); foreach (Person child in bob.Children) { WriteLine($"\t- {child.Name}"); } WriteLine($"{bob.Name} is a {Person.Species}."); WriteLine($"{bob.Name} was born on {bob.HomePlanet}."); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); (string Name, int Number)namedFruit = bob.GetNamedFruit(); WriteLine($"There are {namedFruit.Number} {namedFruit.Name}."); (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName} and {fruitNumber}."); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); WriteLine(bob.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(bob.OptionalParameters(command: "Poke!", active: false)); Person alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine(format: "{0} was born on {1:dd MMM yy}", arg0: alice.Name, arg1: alice.DateOfBirth); BankAccount.InterestRate = 0.012M; BankAccount 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); BankAccount gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; WriteLine(format: "{0} earned {1:C} interest.", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance * BankAccount.InterestRate); Person 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 ); Person 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 ); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); 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}."); int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f does not exist yet."); bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}."); Person 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.Name}'s favorite ice-cream flavor is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "Red"; WriteLine($"{sam.Name}'s favorite primary color is {sam.FavoritePrimaryColor}."); sam.Children.Add(new Person { Name = "Charlie" }); sam.Children.Add(new Person { Name = "Ella" }); WriteLine($"{sam.Name}'s first child is {sam.Children[0].Name}."); WriteLine($"{sam.Name}'s second child is {sam.Children[1].Name}."); WriteLine($"{sam.Name}'s first child is {sam[0].Name}."); WriteLine($"{sam.Name}'s second child is {sam[1].Name}."); object [] passengers = { new FirstClassPassenger { AirMiles = 1_419 },
static void Main(string[] args) { // var person1 = new Person(); // person1.Name = "Juliani"; // person1.DateOfBirth = new System.DateTime(1995, 12, 12); // person1.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; // person1.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon |WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // WriteLine($"{person1.Name} was born on {person1.DateOfBirth :dddd, d MMMM yyyy} and {person1.Name}'s favorite wonder is {person1.FavoriteAncientWonder}"); // // p1.BucketList = (WondersOfTheAncientWorld)18; // WriteLine($"{person1.Name}'s bucket list is {person1.BucketList}"); var person2 = new Person { Name = "Janete", DateOfBirth = new DateTime(1977, 8, 19) }; WriteLine($"{person2.Name} was born on {person2.DateOfBirth :d MMM yy} and is a {Person.Species} from {person2.HomePlanet}"); person2.Children.Add(new Person { Name = "Juliani" }); person2.Children.Add(new Person { Name = "Julia" }); WriteLine( $"{person2.Name} has {person2.Children.Count} children:"); for (int child = 0; child < person2.Children.Count; child++) { WriteLine($" {person2.Children[child].Name}"); } BankAccount.InterestRate = 0.012M; var ba1 = new BankAccount(); ba1.AccountName = "Mrs. Jones"; ba1.Balance = 2400; WriteLine($"{ba1.AccountName} earned {ba1.Balance * BankAccount.InterestRate:C} interest."); var ba2 = new BankAccount(); ba2.AccountName = "Ms. Gerrier"; ba2.Balance = 98; WriteLine($"{ba2.AccountName} earned {ba2.Balance * BankAccount.InterestRate:C} interest."); var p3 = new Person(); WriteLine($"{p3.Name} was instantiated at {p3.Instantiated:hh:mm:ss} on {p3.Instantiated:dddd, d MMMM yyyy}"); var p4 = new Person("Aziz"); WriteLine($"{p4.Name} was instantiated at {p4.Instantiated:hh:mm:ss} on {p4.Instantiated:dddd, d MMMM yyyy}"); p4.WriteToConsole(); WriteLine(p4.GetOrigin()); Tuple <string, int> fruit4 = p4.GetFruitCS4(); WriteLine($"There are {fruit4.Item2} {fruit4.Item1}."); (string, int)fruit7 = p4.GetFruitCS7(); WriteLine($"{fruit7.Item1}, {fruit7.Item2} there are."); var fruitNamed = p4.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} {fruitNamed.Name}?"); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (person2.Name, person2.Children.Count); WriteLine($"{thing2.Item1} has {thing2.Item2} children."); (string fruitName, int fruitNumber) = person2.GetFruitCS7(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(person2.SayHello()); WriteLine(person2.SayHelloTo("Emily")); int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); person2.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); // simplified C# 7 syntax for out parameters int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); person2.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); }
static void Main(string[] args) { var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); WriteLine($"{bob.Name} was born on {bob.DateOfBirth:dddd, d MMMM yyyy}"); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine($"{bob.Name}'s favorite wonder is {bob.FavoriteAncientWonder}. Its integer is {(int)bob.FavoriteAncientWonder}."); bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; WriteLine($"{bob.Name}'s bucket list is {bob.BucketList}"); bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Zoe" }); WriteLine($"{bob.Name} has {bob.Children.Count} children:"); foreach (Person child in bob.Children) { WriteLine($" {child.Name}"); } WriteLine($"{bob.Name} is of species {Person.Species}"); WriteLine($"{bob.Name} was born on planet {bob.HomePlanet}"); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = bob.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}."); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine($"{alice.Name} was born on {alice.DateOfBirth:dd MMM yy}"); BankAccount.InterestRate = 0.012M; var jonesAccount = new BankAccount(); jonesAccount.AccountName = "Mrs. Jones"; jonesAccount.Balance = 2400; WriteLine($"{jonesAccount.AccountName} earned {jonesAccount.Balance * BankAccount.InterestRate:C} interest."); var gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; WriteLine($"{gerrierAccount.AccountName} earned {gerrierAccount.Balance * BankAccount.InterestRate:C}"); var blankPerson = new Person(); WriteLine($"{blankPerson.Name} of {blankPerson.HomePlanet} was created at {blankPerson.Instantiated:hh:mm:ss} on a {blankPerson.Instantiated:dddd}"); var gunny = new Person("Gunny", "Mars"); WriteLine($"{gunny.Name} of {gunny.HomePlanet} was created at {gunny.Instantiated:hh:mm:ss} on a {gunny.Instantiated:dddd}"); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump", 98.5)); WriteLine(bob.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(bob.OptionalParameters("Poke", active: false)); 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}"); int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}!"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavoriteIceCream = "Chocolage 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[0].Name}"); WriteLine($"Sam's first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { Clear(); WriteLine("-- People App --"); var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1956, 8, 24); WriteLine($"{bob.Name} was born on " + $"{bob.DateOfBirth:dddd, d MMMM yyyy}"); WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); WriteLine($"{bob.Name} is a {Person.Species}"); bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Jessica" }); for (int child = 0; child < bob.Children.Count; child++) { WriteLine($"{bob.Children[child].Name} is Bob's Child."); } BankAccount.InterestRate = 0.012M; // Store a shared value var alfredAccount = new BankAccount(); // Instance BankAccount alfredAccount.AccountName = "Mrs. Alfred"; alfredAccount.Balance = 2400; WriteLine(format: "{0} earned {1:C} interest.", arg0: alfredAccount.AccountName, arg1: alfredAccount.Balance * BankAccount.InterestRate); var jessicaAccount = new BankAccount(); // Instance BankAccount jessicaAccount.AccountName = "Ms. Jessica"; jessicaAccount.Balance = 5600; WriteLine(format: "{0} earned {1:C} interest.", arg0: jessicaAccount.AccountName, arg1: jessicaAccount.Balance * BankAccount.InterestRate); // Blank Person for Constructor 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); // Defauls var thingOfDefault = new ThingOfDefaults(); WriteLine("Default Values"); WriteLine($"{thingOfDefault.Population} - {thingOfDefault.When} - " + $"{thingOfDefault.Name} - {thingOfDefault.People}"); // METHOD CALL bob.WriteToConsole(); WriteLine(bob.GetOrigin()); WriteLine(new string('-', 35)); // Return Value var toReturn = new Processor(); var result = toReturn.GetTheData(); WriteLine(result.Text); WriteLine(result.Number); WriteLine(new string('-', 35)); (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); WriteLine(new string('-', 35)); var thing1 = ("Neville", 4); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"ThingOne = {thing1.Item1} has {thing1.Item2} children."); WriteLine($"ThingTwo = {thing2.Name} has {thing2.Count} children."); WriteLine(new string('-', 35)); (string name, int age)deconstruct = bob.GetPerson(); WriteLine($"{deconstruct.name} is {deconstruct.age} yeas old!"); WriteLine(new string('-', 35)); WriteLine(bob.SayHello("Deve")); WriteLine(bob.SayHelloTo("Firat")); WriteLine(gunny.DirtyTalk("Zombi")); WriteLine(gunny.SaySomething("Blah Blash!")); WriteLine(); WriteLine(new string('-', 35)); 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}"); WriteLine(); WriteLine(new string('-', 35)); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; sam.FavoriteIceCream = "Chocolate Fudge"; sam.FavoritePrimaryColor = "Red"; WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}"); WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}"); WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine($"Sam {sam.Age} yeas old!"); WriteLine(); WriteLine(new string('-', 35)); 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 first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); WriteLine(); WriteLine(new string('-', 35)); // ----------------------------------------------------------------- object[] passengers = { new FirstClassPassenger { AirMiles = 1_419 },
static void Main(string[] args) { var bob = new Person(); // WriteLine(bob.ToString()); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeus; bob.BucketList = WondersOfTheAncientWorld.Pyramid | WondersOfTheAncientWorld.StatueOfZeus; // bob.BucketList = (WondersOfTheAncientWorld)5; bob.Children.Add(new Person { Name = "Jeff" }); bob.Children.Add(new Person { Name = "Mary" }); WriteLine($"{bob.Name} was bon on {bob.DateOfBirth:d}"); WriteLine($"{bob.Name} favorite place is {(int)bob.FavoriteAncientWonder}"); WriteLine($"{bob.Name} bucketlist is {bob.BucketList}"); WriteLine($"{bob.Name} has {bob.Children.Count} children"); for (int child = 0; child < bob.Children.Count; child++) { WriteLine(bob.Children[child].Name); } WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} was born in {bob.HomePlanet}"); 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 ); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = bob.GetNamedFruit(); WriteLine($"{fruitNamed.Name}, {fruitNamed.Number} there are."); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("emily")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("jump", 98.5)); WriteLine(bob.OptionalParameters(number: 52.7, command: "Hide")); WriteLine(bob.OptionalParameters("Poke", active: false)); int a = 10, b = 20, 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}"); a = 10; b = 20; WriteLine($"Before: a = {a}, b = {b}, d dose not exist"); bob.PassingParameters(a, ref b, out int d); WriteLine($"After: a = {a}, b = {b}, d = {d}"); BankAccount.IntrestRate = 0.012M; 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.IntrestRate); 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"; sam.FavoritePrimaryColor = "red"; WriteLine($"sam's Favorite icecream is {sam.FavoriteIceCream}"); WriteLine($"sam's Favorite color is {sam.FavoritePrimaryColor}"); }
static void Main(string [] args) { var p1 = new Person(); p1.Name = "Bob Smith"; p1.DateOfBirth = new System.DateTime(1965, 12, 22); p1.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.TempleOfArtemisAtEphesus; //WriteLine ( $"{p1.Name} was born on {p1.DateOfBirth: dddd, d MMMM yyyy}" ); WriteLine($"{p1.Name}'s favourite wonder is {p1.BucketList}"); WriteLine($"{p1.Name} is a {Person.Species}."); //WriteLine($"{p1.Name} was born on {p1.HomePlanet}"); p1.WriteToConsole(); WriteLine(p1.GetOrigin()); WriteLine(p1.SayHello()); WriteLine(p1.SayHello("Emily")); WriteLine(p1.OptionalParameters("Jump!", 98.5)); p1.Children.Add(new Person { Name = "Alfred" }); p1.Children.Add(new Person { Name = "Zoe" }); WriteLine($"{p1.Name} has {p1.Children.Count} children:"); // for ( int child = 0; child < p1.Children.Count; child++ ) // { // WriteLine($" {p1.Children[child].Name}"); // } foreach (var child in p1.Children) { WriteLine($" {child.Name}"); } BankAccount.InterestRate = 0.012M; var ba1 = new BankAccount(); ba1.AccountName = "Mrs. Jones"; ba1.Balance = 2400; WriteLine($"{ba1.AccountName} earned {ba1.Balance * BankAccount.InterestRate:C} interest."); var ba2 = new BankAccount(); ba2.AccountName = "Ms. Gerrier"; ba2.Balance = 98; WriteLine($"{ba2.AccountName} earned {ba2.Balance * BankAccount.InterestRate:C} interest."); var p3 = new Person(); WriteLine($"{p3.Name} was instantiated at {p3.Instantiated:h:mm:ss tt zz} on {p3.Instantiated:D}"); var p4 = new Person("Aziz"); WriteLine($"{p4.Name} was instantiated at {p4.Instantiated:h:mm:ss tt} on {p4.Instantiated:D}"); // Tuples WriteLine("\nUsing Tuples..."); Tuple <string, int> fruit4 = p1.GetFruitCS4(); WriteLine($"There are {fruit4.Item1} {fruit4.Item2}."); // (string, int) fruit7 = p1.GetFruitCS7 (); // WriteLine($"{fruit7.Name}, {fruit7.Number} there are."); var fruitNamed = p1.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} {fruitNamed.Name}?"); // Tuple name inference WriteLine("\nUsing Tuple name inferencing..."); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (p1.Name, p1.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); // Deconstructing a Tuple for its individual values. WriteLine("\nDeconstructing a tuple..."); (string fruitName, int fruitNumber) = p1.GetFruitCS7(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); // Passing parameters into functions int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); p1.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); p1.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavouriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favourite ice-cream flavor is {sam.FavouriteIceCream}."); sam.FavouritePrimaryColor = "Red"; WriteLine($"Sam's favourite primary color is {sam.FavouritePrimaryColor}."); 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 first child is {sam [ 0 ].Name}"); WriteLine($"Sam's second child is {sam [ 1 ].Name}"); }
static void Main(string[] args) { var blankPerson = new Person("Anonymous", "Somewhere"); WriteLine($"{blankPerson.Name} was created at {blankPerson.Instantiated}"); int a = 10; int b = 20; int c = 30; var bob = new Person("Bob", "Earth"); bob.Name = "Bob"; bob.DateOfBirth = new DateTime(1965, 12, 22); bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; bob.WriteToConsole(); WriteLine(bob.GetOrigin()); var meaningOfLife = new Processor(); (string Name, int Number)answer = meaningOfLife.GetTheData(); WriteLine($"{answer.Name} -> {answer.Number}"); 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("Sam", Convert.ToString(new DateTime(1972, 1, 27))); WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); // BankAccount.InterestRate = 0.012m; // var jonesAccount = new BankAccount(); // jonesAccount.AccountName = "Mrs. Johnes"; // jonesAccount.Balance = 2400; // WriteLine($"{bob.Name} is {Person.Species} was born on {bob.PlanetOfBorn}"); // WriteLine(format: "{0} earned {1:C} interest.", // arg0: jonesAccount.AccountName, // arg1: jonesAccount.Balance * BankAccount.InterestRate); // var gerrierAccount = new BankAccount(); // gerrierAccount.AccountName = "Ms. Gerrier"; // gerrierAccount.Balance = 98; // WriteLine(format: "{0} earned {1:C} interest.", // arg0: gerrierAccount.AccountName, // arg1: gerrierAccount.Balance * BankAccount.InterestRate); // bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; // bob.BucketList = (WondersOfTheAncientWorld)18; // WriteLine($"{bob.Name}'s bucket list is {bob.BucketList}"); // WriteLine( // format: "{0} was born on {1:dddd, d MMMM yyyy}", // arg0: bob.Name, // arg1: bob.DateOfBirth // ); // WriteLine( // format: "{0}'s favorite wonder is {1}. It's integer is {2}", // arg0: bob.Name, // arg1: bob.FavoriteAncientWonder, // arg2: (int)bob.FavoriteAncientWonder // ); // bob.Children.Add(new Person {Name = "Alfred"}); // bob.Children.Add(new Person {Name = "Zoe"}); // WriteLine($"{bob.Name} has {bob.Children.Count} children: "); // foreach (Person child in bob.Children) // { // WriteLine($"{child.Name}"); // } }
static void Main(string[] args) { var john = new Person("John", "Earth"); john.Name = "John Doe"; john.DateOfBirth = new DateTime(1992, 6, 19); WriteLine(format: "{0} was born on {1:dddd, d MMMM yyyy}", // dddd: name of day, d: number of day, MMMM: name of month, yyyy: full year arg0: john.Name, arg1: john.DateOfBirth); WriteLine(format: "{0} was born on {1:dd MMM yy}", arg0: john.Name, arg1: john.DateOfBirth); WriteLine(); // ==========Enums========== // Using enums in PacktLibrary WondersOfTheAncientWorld.cs john.FavoriteWonder = WondersOfTheAncientWorld.HangingGardensOfBabylon; WriteLine(format: "{0}'s favorite wonder is {1}. It's integer is {2}", arg0: john.Name, arg1: john.FavoriteWonder, arg2: (int)john.FavoriteWonder); // enum valuesa are initialy stored as an int for efficiency. // enums can also be called and set using int values when useing the decoration [System.Flags] and inheriting from byte john.BucketList = WondersOfTheAncientWorld.ColossusOfRhodes | WondersOfTheAncientWorld.GreatPyramidOfGiza; WriteLine($"{john.Name}'s bucket list is {john.BucketList}"); john.BucketList = (WondersOfTheAncientWorld)33; WriteLine($"{john.Name}'s bucket list is {john.BucketList}"); WriteLine(); // ==========Objects with Lists========== // calling from the public List<Person> in PacktLibrary Person.cs john.Children.Add(new Person("Jane", john.HomePlanet)); john.Children.Add(new Person("Jack", john.HomePlanet)); WriteLine($"{john.Name} has {john.Children.Count} children:"); for (int child = 0; child < john.Children.Count; child++) { WriteLine($" {john.Children[child].Name}"); } WriteLine(); // ==========Using static values========== // using BankAccount.cs class BankAccount.InterestRate = 0.12M; // store a shared value, InterestRate is static var doeAccount = new BankAccount(); doeAccount.AccountName = "Mr. Doe"; doeAccount.Balance = 43_600; WriteLine(format: "{0} earned {1:C} interest.", // :C - currency format arg0: doeAccount.AccountName, arg1: doeAccount.Balance * BankAccount.InterestRate); var smithAccount = new BankAccount(); smithAccount.AccountName = "Mr. Smith"; smithAccount.Balance = 1_400; WriteLine(format: "{0} earned {1:C} interest.", // :C - currency format arg0: smithAccount.AccountName, arg1: smithAccount.Balance * BankAccount.InterestRate); // using const value from Person.cs WriteLine($"{john.Name} is a {Person.Species}. (We hope)"); // using readonly value from Person.cs WriteLine($"{john.Name} is from {john.HomePlanet}. (If he is a H**o Sapien)"); WriteLine(); // ==========Contrcutors========== // Contrcutors with arguments // using PersonArgs.cs var mike = new Person("Mike", "Mars"); WriteLine(format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", arg0: mike.Name, arg1: mike.HomePlanet, arg2: mike.Instantiated); // using methods mike.WriteToConsole(); WriteLine(mike.GetOrigin()); WriteLine(); // ==========Tuples========== // using tuples (string, int)fruit = mike.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = mike.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}."); // variable use of tuples var thing1 = ("George", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} Children."); var thing2 = (john.Name, john.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); // Deconstructing tuples (string fruitName, int fruitNumber) = mike.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); // taking the values held in GetFruit() and assigning the tuple to the new variables WriteLine(); // ==========Param Methods and Overloading========== WriteLine(mike.SayHello()); WriteLine(mike.SayHello("John")); WriteLine(); // ==========Optional Parameters========== WriteLine(mike.OptionalParameters()); // will output default values in PersonArgs.cs WriteLine(mike.OptionalParameters("Jump!", 98.5)); // overwrites the default values // delcaring some parameters can be skipped if you name the parameters based on the method WriteLine(mike.OptionalParameters(number: 29.1, command: "Hide!")); // parameter calues can be named when declaring them WriteLine(mike.OptionalParameters("Walk!", active: false)); WriteLine(); // ==========Controlling How Parameters are Passed========== // by value (default)(in), ref(in-out), and as an out parameter(out) int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}."); mike.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}."); // When passing a value as a parameter by default, the current VALUE // is passed, not the variable itself. a before and after is 10. // When passing y as a reference to b, b increments as y increments. // When using an out, the value in z overwrites the value of c. // In C# 7.0 we can simpify code that uses the out variable: int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); // simplified syntax for the out parameter mike.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f ={f}"); WriteLine(); // ==========Using Properties in C# 6+========== // Using PersonAutoGen.cs var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1928, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); // settable properties sam.FavoriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favorite ice cream is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "red"; WriteLine($"Sam's favorite color is {sam.FavoritePrimaryColor}."); // ==========Indexers========== 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 first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { var edixon = new Person(); edixon.Name = "Edixon van Vliet"; edixon.DateOfBirth = new DateTime(1996, 3, 27); WriteLine( "{0} was born on {1:dddd, d MMMM yyyy}", edixon.Name, edixon.DateOfBirth ); edixon.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine( "{0}'s favorite wonder is {1}. It's integer is {2}.", edixon.Name, edixon.FavoriteAncientWonder, (int)edixon.FavoriteAncientWonder ); edixon.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; WriteLine($"{edixon.Name}'s bucket list is {edixon.BucketList}"); edixon.Children.Add(new Person { Name = "Alfred" }); edixon.Children.Add(new Person { Name = "Zoe" }); WriteLine($"{edixon.Name} has {edixon.Children.Count} children:"); foreach (Person child in edixon.Children) { WriteLine($" {child.Name}"); } BankAccount.InterestRate = 0.012m; var jonesAccount = new BankAccount(); jonesAccount.AccountName = "Mrs. Jones"; jonesAccount.Balance = 2400m; WriteLine( "{0} earned {1:c} interest.", jonesAccount.AccountName, jonesAccount.Balance * BankAccount.InterestRate ); var gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98m; WriteLine( "{0} earned {1:c} interest.", gerrierAccount.AccountName, gerrierAccount.Balance * BankAccount.InterestRate ); WriteLine($"{edixon.Name} is a {Person.Species}"); WriteLine($"{edixon.Name} was born on {edixon.HomePlanet}"); var blankPerson = new Person(); WriteLine(format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", blankPerson.Name, blankPerson.HomePlanet, blankPerson.Instantiated ); var gunny = new Person("Gunny", "Mars"); WriteLine( "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", gunny.Name, gunny.HomePlanet, gunny.Instantiated ); edixon.WriteToConsole(); WriteLine(edixon.GetOrigin()); (string, int)fruit = edixon.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = edixon.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}"); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (edixon.Name, edixon.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); (string fruitName, int fruitNumber) = edixon.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(edixon.SayHello()); WriteLine(edixon.SayHello("Emily")); WriteLine(edixon.OptionalParameters()); WriteLine(edixon.OptionalParameters("Jump!", 98.5)); WriteLine(edixon.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(edixon.OptionalParameters("Poke!", active: false)); int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); edixon.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); int d = 10; int e = 20; WriteLine( $"Before: d = {d}, e = {e}, f doesn't exist yet!"); // simplified C# 7.0 syntax for the out parameter edixon.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); 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.Name}'s favorite ice-cream flavor is {sam.FavoriteIceCream}." ); sam.FavoritePrimaryColor = "Red"; WriteLine( $"{sam.Name}'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 first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1954, 12, 22); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; bob.BucketList = (WondersOfTheAncientWorld)18; bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Suzie" }); WriteLine( format: "{0} was born on {1:dddd, d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth ); WriteLine( format: "{0}'s favorite wonder is {1}. It's integer is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder ); WriteLine($"{bob.Name}'s bucket list is {bob.BucketList}"); WriteLine($"Bob has {bob.Children.Count} children."); for (int child = 0; child < bob.Children.Count; child++) { WriteLine($"{bob.Children[child].Name}"); } ; foreach (Person child in bob.Children) { WriteLine($"Saying hello to {child.Name}!"); } ; WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine( format: "{0} was born on {1:dd MMM yy}", arg0: alice.Name, arg1: alice.DateOfBirth ); // Bank Accounts BankAccount.InterestRate = 0.012M; // storing a 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 leslieAccount = new BankAccount(); leslieAccount.AccountName = "Mr. Alldridge"; leslieAccount.Balance = 100; WriteLine(format: "{0} earned {1:C} interest.", arg0: leslieAccount.AccountName, arg1: leslieAccount.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 ); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var namedFruit = bob.GetNamedFruit(); WriteLine($"{namedFruit.Name} and {namedFruit.Number} exist"); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); // infers Name and Count as the named fields // deconstructing tuples (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed {fruitName}, {fruitNumber}"); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Leslie")); WriteLine(bob.OptionalParams()); WriteLine(bob.OptionalParams(command: "Jump", number: 99)); WriteLine(bob.OptionalParams("Poke!", active: false)); int a = 10; int b = 20; WriteLine($"Before: a = {a}, b = {b}, c = doesn't exist"); bob.PassingParameters(a, ref b, out int c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1992, 6, 9) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavouriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favourite ice-cream flavour is {sam.FavouriteIceCream}."); sam.FavouritePrimaryColor = "Red"; WriteLine($"Sam's favourite primary color is {sam.FavouritePrimaryColor}"); 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 first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { //Bob is instantiated and then his fields are set later var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // equivalent to bob.BucketList = (WondersOfTheAncientWorld)18 due to byte bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Zoe" }); WriteLine( format: "{0} was born on {1:dddd, d MMMM yyyy}.", arg0: bob.Name, arg1: bob.DateOfBirth ); WriteLine( format: "{0}'s favorite wonder is {1}. Its integer is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder ); WriteLine($"{bob.Name}'s bucket list is {bob.BucketList}"); WriteLine($"{bob.Name} has {bob.Children.Count} children:"); //using a for loop to get the name of the children /* for (int child = 0; child < bob.Children.Count; child++) { * WriteLine($" {bob.Children[child].Name}"); * } */ //using a foreach loop to iterate through the list foreach (Person child in bob.Children) { WriteLine($" {child.Name}"); } WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); //Alice is instantiated with the fields set at that time var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine( format: "{0} was born on {1:dd MMM yy}", arg0: alice.Name, arg1: alice.DateOfBirth ); BankAccount.InterestRate = 0.012M; //store a shared value var jonesAccount = new BankAccount(); jonesAccount.AccountName = "Mrs. Jones"; jonesAccount.Balance = 2400; decimal jonesInterest = jonesAccount.Balance * BankAccount.InterestRate; WriteLine($"{jonesAccount.AccountName} earned {jonesInterest:C} interest."); var gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; decimal gerrierInterest = (gerrierAccount.Balance * BankAccount.InterestRate); WriteLine($"{gerrierAccount.AccountName} earned {gerrierInterest:C} interest."); var blankPerson = new Person(); WriteLine($"{blankPerson.Name} of {blankPerson.HomePlanet} was created at " + $"{blankPerson.Instantiated:hh:mm:ss} on a {blankPerson.Instantiated:dddd}."); var gunny = new Person("Gunny", "Mars"); WriteLine($"{gunny.Name} of {gunny.HomePlanet} was created at " + $"{gunny.Instantiated:hh:mm:ss} on a {gunny.Instantiated:dddd}."); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); //item syntax (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); //named syntax var fruitNamed = bob.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}"); //using the item1, item2 syntax var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); //set these equal to the class fields var thing2 = (bob.Name, bob.Children.Count); //can reference it using those automatically WriteLine($"{thing2.Name} has {thing2.Count} children."); (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); WriteLine(bob.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(bob.OptionalParameters("Poke!", active: false)); 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}"); int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); //simplified C# 7.0 syntax for the out parameter bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); 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 first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { //var bob =new Person(); //WriteLine(bob.ToString()); var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); //dddd means the name of the day of the week //d means the ne number of the day of the month //MMMM means the name of the month //yyyy means the full number of the year WriteLine(format: "{0} was born on {1:dddd, d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); // another way to initialize the object var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine(format: "{0} was born on {1:dd MMM yy}", arg0: alice.Name, arg1: alice.DateOfBirth); // Use an enum // enum internally is stored as int for effiency bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlimpia; WriteLine(format: "{0}'s favorite wonder is {1}. It's integer is {2}", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder); bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; //bob.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{bob.Name}'s bucket list is {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}"); } // Bank Account Example BankAccount.InterestRate = 0.012M; // store a 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 gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; WriteLine(format: "{0} earned {1:C} interest.", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance * BankAccount.InterestRate); //Using const value WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); // Constructor initialization 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); // Second constructor to initialize values 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); // calling methods bob.WriteToConsole(); WriteLine(bob.GetOrigin()); // Get Tuple's fields (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); // Named Tuple var fruitNamed = bob.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}."); // Examples of tuples var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2}"); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count}"); // Deconstructing tuples (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); // Method paramters WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); // Optional parameters WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); // Named paaramters WriteLine(bob.OptionalParameters(number: 52.7, command: "Hide!")); // You can even use named parameters to skip over optional parameters WriteLine(bob.OptionalParameters("Poke!", active: false)); // in - ref - out parameters 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}"); // More example about out parameters int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet"); // Simplified C# 7.0 syntax for the out parameter bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); // Working with Properties 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 icecream flavor is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "blue"; WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); // Indexers 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}"); // Using Indexers WriteLine($"Sam's first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }