static void Main(string[] args) { var bob = new Person(); bob.Name = "Bob"; bob.DateOfBirth = new DateTime(1965, 12, 22); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.ColosusOfRhodes; bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.ColosusOfRhodes; // bob.BucketList = (WondersOfTheAncientWorld)18; bob.Childred = new List <Person> { new Person { Name = "Zoe" }, new Person { Name = "Alfred" } }; (string, int)fruit = bob.GetFruit(); var fruitByName = bob.GetFruitByName(); (string fruitName, int fruitCount) = bob.GetFruit(); WriteLine( format: "{0} - {1}", arg0: fruit.Item1, arg1: fruit.Item2 ); WriteLine( format: "By name: {0} - {1}", arg0: fruitByName.Name, arg1: fruitByName.Count ); WriteLine( format: "{0} was born on {1:dddd, d MMMM yyyy}. Favorite wonder: {2}", arg0: bob.Name, arg1: bob.DateOfBirth, arg2: bob.FavoriteAncientWonder ); WriteLine( format: "Favorite wonders: {0}", arg0: bob.BucketList ); WriteLine( format: "Childrens: {0}", arg0: bob.Childred ); }
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) { // 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); 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) { 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(); //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}"); }
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) { // 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) { 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) { 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(); 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 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) { //bob 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; //bob.BucketList = (WondersOfTheAncientWorld)18; is the same as above but not as clear bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Zoe" }); //call GetFruit method and then output the tuple's fields (string, int)fruit = bob.GetFruit(); // WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); //call GetNamedFruit method which is a tuple with named fields var fruitNamed = bob.GetNamedFruit(); // WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}."); // WriteLine($"{bob.Name} was born on {bob.DateOfBirth: dddd, d MMMM yyyy}"); // WriteLine($"{bob.Name}'s favorite wonder is {bob.FavoriteAncientWonder}. It's integer is {(int)bob.FavoriteAncientWonder}."); // WriteLine($"{bob.Name}'s bucket list is {bob.BucketList}"); // WriteLine($"{bob.Name} has {bob.Children.Count} children:"); // WriteLine($"{bob.Name} is a {Person.Species}."); // WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); // for (int child = 0; child < bob.Children.Count; child++) // { // WriteLine($" {bob.Children[child].Name}"); // } //alice 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; //store a shared value 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} interest."); //instantiate new person and then output its initial field values (p.160) var blankPerson = new Person(); // WriteLine($"{blankPerson.Name} of {blankPerson.HomePlanet} was created at {blankPerson.Instantiated:hh:mm:ss} on a {blankPerson.Instantiated:dddd}"); //using the 2nd constructor to set initial values var gunny = new Person("Gunny", "Mars"); // WriteLine($"{gunny.Name} of {gunny.HomePlanet} was creatd at {gunny.Instantiated:hh:mm:ss} on a {gunny.Instantiated:dddd}."); //call the two methods from the Person class // bob.WriteToConsole(); // WriteLine(bob.GetOrigin()); //create two tuples made of a string and int value //In C# 7.1 and later, the second thing can infer the names Name and Count // 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"); //deconstruct return values into two separate variables from tuple (string fruitName, int fruitNumber) = bob.GetFruit(); // WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); //call two methods from Person class and write return values to console // WriteLine(bob.SayHello()); // WriteLine(bob.SayHello("Emily")); //call optional parameters method from Person class // WriteLine(bob.OptionalParameters()); //pass values for command and number parameters // WriteLine(bob.OptionalParameters("Jump!", 98.5)); //using named parameters so the order can be swapped // WriteLine(bob.OptionalParameters(number: 52.7, command: "Hide!")); //using positional order for the command param, skipping the number param, and using the named active param // WriteLine(bob.OptionalParameters("Poke!", active: false)); //pass variables into PassingParameters method // 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}"); //simplify the code that uses the out variable in C# 7.0 and later by declaring inline // 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}"); //DEFINING READONLY PROPERTIES (172) var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; // WriteLine(sam.Origin); // WriteLine(sam.Greeting); // WriteLine(sam.Age); //DEFINING SETTABLE PROPERTIES //add statements to set Sam's favorite ice cream and color, and then write them to the console 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}"); //Good Practice: Only use indexers if it makes sense to use the square bracket, also known as array syntax. As you can see from the preceding example, indexers rarely add much value }
static void Main(string[] args) { //Longhand instantiation var ant = new Person(); ant.szLastName = "Jim"; ant.szFirstName = "Anthony"; ant.dtDateofBirth = new DateTime(1996, 06, 17); // WriteLine( // format: "{0} {1} was born on {2:dddd, d MMMM yyyy}", // arg0: ant.szFirstName, // arg1: ant.szLastName, // arg2: ant.dtDateofBirth); //Shorthand intitalisation: var jen = new Person() { //shorthand instantiation szFirstName = "Jennifer", szLastName = "Jim", dtDateofBirth = new DateTime(1954, 12, 27), FavouriteAncientWonder = WondersOfTheWorld.TempleOfArtemisAtEphesus, BucketList = WondersOfTheWorld.StatueofZeusAtOlympia | WondersOfTheWorld.ColossusOfRhodes }; // WriteLine // ( // $"{jen.szFirstName}'s bucket list of wonders to visit is {jen.BucketList}" // ); //------------------------------------------------------------------------------ // //Adding collections jen.Children.Add(new Person { szFirstName = "Shit_one", szLastName = jen.szLastName } ); jen.Children.Add(new Person { szFirstName = "Shit_two", szLastName = jen.szLastName } ); //Foreach loop on collections & enumerables // WriteLine($"{jen.szFirstName} has {jen.Children.Count} children:"); // foreach(Person child in jen.Children) // { // WriteLine($"{child.szFirstName}"); // } // var wonders = Enum.GetValues(typeof(WondersOfTheWorld)); // foreach(var i in wonders) // { // WriteLine(i); // } //------------------------------------------------------------------------------ // Setting static fields //fields BankAccount.InterestRate = 0.012M; //Stores a shared value var antsAccount = new BankAccount(); antsAccount.AccountName = "Mr. Kcoon Leung Jim"; antsAccount.Balance = 2300; // WriteLine( // format: "{0} earned {1:c} in interest", // arg0: antsAccount.AccountName, // arg1: antsAccount.Balance * BankAccount.InterestRate // ); //const values are drawn from the class type //WriteLine($"{ant.szFirstName} is a {Person.szSpecies}"); //readonly values are drawn from the object instance as this is inherited //WriteLine($"{ant.szFirstName}'s home planet is {ant.szHomePlanet}"); //Generate new person and identify default values: var newCharacter = new Person(); //WriteLine($"newCharacter: {newCharacter.szFirstName} was created at {newCharacter.dtCreatedDate:hh:mm:ss} on {newCharacter.dtCreatedDate:dd:MMMM:yyyy}"); var Waggas = new Person("Waggas", "Jerob", "Pluto"); // WriteLine( // format: "{0} {1} is an alien from {2}", // arg0: Waggas.szFirstName, // arg1: Waggas.szLastName, // arg2: Waggas.szHomePlanet // ); // Console.WriteLine($"default(int) = {default(int)}"); // Console.WriteLine($"default(bool) = {default(bool)}"); // Console.WriteLine($"default(DateTime) = {default(DateTime)}"); // Console.WriteLine($"default(string) = {default(string)}"); var defaultThing = new Defaults(); // WriteLine( // $"{defaultThing.Population}\n{defaultThing.When}\n{defaultThing.Name}\n{defaultThing.People}" // ); //Defining Methods //ant.Print(); // WriteLine(ant.GetOrigin()); /*------------------------------------*/ //Exploring tuples (string, int)AntsFruit = ant.GetFruit(4); //WriteLine($"{ant.szFirstName} got {AntsFruit.Item2} {AntsFruit.Item1}(s)!"); var Gear = Person.GetLenses(); // WriteLine($"{ant.szFirstName} got {Gear.quant} {Gear.lensType} lenses!"); (string lensType, int quant) = Person.GetLenses(); //WriteLine($"Deconstructed: {lensType},{quant}"); /*------------------------------------*/ //method overload // WriteLine(ant.SayHello()); // WriteLine(ant.SayHello("Emily")); // WriteLine(ant.SayHello(10)); /*------------------------------------*/ //optional parameters // WriteLine(ant.OptionalParam(number: 23.5, active:false,command:"lift!")); /*------------------------------------*/ //Param control int a = 10; int b = 10; int c = 10; // WriteLine($"Before: a={a},b={b},c={c}"); Person.ParamControl(a, ref b, out c); // WriteLine($"After: a={a},b={b},c={c}"); Person.outParam(out int z); //generate z varibable // WriteLine(z); //print /*--------------------------------*/ //Access control var danny = new Person() { szFirstName = "Daniel", dtDateofBirth = new DateTime(1900, 01, 01) }; // WriteLine(danny.Origin); // WriteLine(danny.greeting("Peter")); // WriteLine(danny.szAge); //setters danny.szFavouriteIcecream = "chocolate"; //WriteLine(danny.szFavouriteIcecream); //Complex setters //danny.szFavouritePrimaryColour = "RED"; danny.FavouritePrimaryColour = "RED"; //WriteLine($"{danny.szFirstName}'s favourite primary colour is {danny.FavouritePrimaryColour}."); /*--------------------------------*/ //Index control // ant.Children.Add(new Person{szFirstName = "David" }); // ant.Children.Add(new Person{szFirstName = "Goliath" }); // WriteLine(ant.Children[0].szFirstName); // WriteLine(ant.Children[1].szFirstName); }
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) { 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) { 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) { //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(); Bob.Name = "Bob Smith"; Bob.DateOfBirth = new DateTime(1965, 12, 22); Bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; Bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // or // Bob.BucketList = (WondersOfTheAncientWorld)18; 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 ); WriteLine( "{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" }); Console.WriteLine($"{Bob.Name} has {Bob.Children.Count} children:"); foreach (var Child in Bob.Children) { WriteLine($" {Child.Name}"); } BankAccount.InterestRate = 0.012M; // var jonesAccount = new BankAccount(); // jonesAccount.AccountName = "Mrs. Jones"; // jonesAccount.Balance = 2400; BankAccount jonesAccount = new BankAccount { AccountName = "Mrs. Jones", 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; BankAccount gerrierAccount = new BankAccount { AccountName = "Ms. Gerrier", 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}."); // Using ValueTuple (not Tuple) var test = (11, 22, 34, 45, 15, 12); WriteLine(test.GetType()); // ValueTuple mutable - Tuple immutable test.Item2 += 11; WriteLine(test); // Buldin tuple by future named "tuple name inference" var thing1 = ("Neville", 4); // C# 7.0 WriteLine($"{thing1.Item1} has {thing1.Item2} children"); // Name by last names of fields C# 7.1 var thing2 = (Bob.Name, Bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children"); var sum = 4.5; var count = 3; var test3 = (sum, count); Console.WriteLine($"Sum of {test3.count} elements is {test3.sum}."); // Deconstruction example (string fruitName, int fruitNumber) = Bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); // Accessible to use tuple with large number of elements var t = (11, 22, 34, 45, 15, 12); WriteLine(t); // Should be 45 WriteLine(t.Item4); //var a = 1; //var test2 = (a, b: 2, 3); //Console.WriteLine($"The 1st element is {test2.Item1} (same as {test2.a})."); //Console.WriteLine($"The 2nd element is {test2.Item2} (same as {test2.b})."); //Console.WriteLine($"The 3rd element is {test2.Item3}."); WriteLine(Bob.SayHello()); WriteLine(Bob.SayHello("Emily")); WriteLine(Bob.OptionalParametrs()); WriteLine(Bob.OptionalParametrs("Jump!", 98.5)); // Optional parametrs often combined with naming parametrs WriteLine(Bob.OptionalParametrs( number: 52.7, command: "Hide!" )); // Can use optional parametrs, named parametrs and simple parametrs WriteLine(Bob.OptionalParametrs("Poke!" /* - Parametr*/, active: false /* - Named parametr and then optional parametr number*/)); int a = 10; int b = 20; int c = 30; WriteLine($"Before: = {a}, b = {b}, c = {c}"); Bob.PassingParametrs(a, ref b, out c); WriteLine($"After: = {a}, b = {b}, c = {c}"); // In C# 7.0 we can simplify code 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 parametr Bob.PassingParametrs(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}"); }