private static void DemoMethodWithTuples(Person p1) { // Exercise: Define methods with tuples Tuple <string, int> fruit4 = p1.GetFruitCS4(); WriteLine($"There are {fruit4.Item2} {fruit4.Item1}."); (string, int)fruit7 = p1.GetFruitCS7(); WriteLine($"{fruit7.Item1}, {fruit7.Item2} there are."); // Exercise: Specify the name of the fields var fruitNamed = p1.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} { fruitNamed.Name}?"); //Exercise: Deconstruct tuples into separate variables (string fruitName, int fruitNumber) = p1.GetFruitCS7(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); }
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) { Person bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 2); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; WriteLine(format: "{0} was born on {1:dddd, dd MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); WriteLine(format: "{0}'s favorite wonder is {1}. Its byte is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (byte)bob.FavoriteAncientWonder); bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Zoe" }); WriteLine($"{bob.Name} has {bob.Children.Count} children:"); foreach (Person child in bob.Children) { WriteLine($"\t- {child.Name}"); } WriteLine($"{bob.Name} is a {Person.Species}."); WriteLine($"{bob.Name} was born on {bob.HomePlanet}."); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); (string Name, int Number)namedFruit = bob.GetNamedFruit(); WriteLine($"There are {namedFruit.Number} {namedFruit.Name}."); (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName} and {fruitNumber}."); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); WriteLine(bob.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(bob.OptionalParameters(command: "Poke!", active: false)); Person alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine(format: "{0} was born on {1:dd MMM yy}", arg0: alice.Name, arg1: alice.DateOfBirth); BankAccount.InterestRate = 0.012M; BankAccount jonesAccount = new BankAccount(); jonesAccount.AccountName = "Mrs. Jones"; jonesAccount.Balance = 2400; WriteLine(format: "{0} earned {1:C} interest.", arg0: jonesAccount.AccountName, arg1: jonesAccount.Balance * BankAccount.InterestRate); BankAccount gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; WriteLine(format: "{0} earned {1:C} interest.", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance * BankAccount.InterestRate); Person blankPerson = new Person(); WriteLine(format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", arg0: blankPerson.Name, arg1: blankPerson.HomePlanet, arg2: blankPerson.Instantiated ); Person gunny = new Person("Gunny", "Mars"); WriteLine(format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", arg0: gunny.Name, arg1: gunny.HomePlanet, arg2: gunny.Instantiated ); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}."); bob.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}."); int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f does not exist yet."); bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}."); Person sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavoriteIceCream = "Chocolate Fudge"; WriteLine($"{sam.Name}'s favorite ice-cream flavor is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "Red"; WriteLine($"{sam.Name}'s favorite primary color is {sam.FavoritePrimaryColor}."); sam.Children.Add(new Person { Name = "Charlie" }); sam.Children.Add(new Person { Name = "Ella" }); WriteLine($"{sam.Name}'s first child is {sam.Children[0].Name}."); WriteLine($"{sam.Name}'s second child is {sam.Children[1].Name}."); WriteLine($"{sam.Name}'s first child is {sam[0].Name}."); WriteLine($"{sam.Name}'s second child is {sam[1].Name}."); object [] passengers = { new FirstClassPassenger { AirMiles = 1_419 },
static void Main(string[] args) { var p1 = new Person(); p1.Name = "Bob Smith"; p1.DateOfBirth = new System.DateTime(1965, 12, 22); WriteLine($"{p1.Name} was born on {p1.DateOfBirth:dddd, d MMMM yyyy}"); var p2 = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 17) }; WriteLine($"{p2.Name} was born on {p2.DateOfBirth:d MMM yy}"); p1.BucketList = WondersOfTheAncientWorld.HangingGardenOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // p1.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{p1.Name}'s bucket list is {p1.BucketList}"); p1.Children.Add(new Person { Name = "Alfred" }); p1.Children.Add(new Person { Name = "Zoe" }); WriteLine( $"{p1.Name} has {p1.Children.Count} children:" ); for (int child = 0; child < p1.Children.Count; child++) { WriteLine($" {p1.Children[child].Name}"); } BankAccount.InterestRate = 0.012M; var ba1 = new BankAccount(); ba1.AccountName = "Mrs. Jones"; ba1.Balance = 2400; WriteLine($"{ba1.AccountName} earned {ba1.Balance * BankAccount.InterestRate:C} interest."); var ba2 = new BankAccount(); ba2.AccountName = "Ms. Gerrier"; ba2.Balance = 98; WriteLine($"{ba2.AccountName} earned {ba2.Balance * BankAccount.InterestRate:C} interest."); WriteLine($"{p1.Name} is a {Person.Species}"); // WriteLine($"{p1.Name} is a {p1.Species}"); WriteLine($"{p1.Name} was born on {p1.HomePlanet}"); var p3 = new Person(); WriteLine($"{p3.Name} was instantiated at {p3.Instantiated:hh:mm:ss} on {p3.Instantiated:dddd, d MMMM yyyy}"); var p4 = new Person("Aziz"); WriteLine($"{p4.Name} was instantiated at {p4.Instantiated:hh:mm:ss} on {p4.Instantiated:dddd, d MMMM yyyy}"); p1.WriteToConsole(); WriteLine(p1.GetOrigin()); Tuple <string, int> fruit4 = p1.GetFruitCS4(); WriteLine($"There are {fruit4.Item2} {fruit4.Item1}."); (string, int)fruit7 = p1.GetFruitCS7(); WriteLine($"{fruit7.Item1}, {fruit7.Item2} there are."); var fruitNamed = p1.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} {fruitNamed.Name}?"); var thing1 = ("Neville", 4); WriteLine( $"{thing1.Item1} has {thing1.Item2} children." ); var thing2 = (p1.Name, p1.Children.Count); WriteLine( $"{thing2.Name} has {thing2.Count} children." ); (string fruitName, int fruitNumber) = p1.GetFruitCS7(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(p1.SayHello()); WriteLine(p1.SayHello("Emily")); WriteLine(p1.OptionalParameters()); WriteLine(p1.OptionalParameters("Jump!", 98.5)); WriteLine(p1.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(p1.OptionalParameters("Poke!", active: false)); int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); p1.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); // simplified C# 7 syntax for out parameters int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); p1.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavoriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "Red"; WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); sam.Children.Add(new Person { Name = "Charlie" }); sam.Children.Add(new Person { Name = "Ella" }); WriteLine($"Sam's first child is {sam.Children[0].Name}"); WriteLine($"Sam's second child is {sam.Children[1].Name}"); WriteLine($"Sam's first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); var harry = new Person { Name = "Harry" }; var mary = new Person { Name = "Mary" }; var jill = new Person { Name = "Jill" }; // call instance method var baby1 = mary.ProcreateWith(harry); // call static method var baby2 = Person.Procreate(harry, jill); // call an operator var baby3 = harry * mary; WriteLine($"{mary.Name} has {mary.Children.Count} children."); WriteLine($"{harry.Name} has {harry.Children.Count} children."); WriteLine($"{jill.Name} has {jill.Children.Count} children."); WriteLine($"{mary.Name}'s first child is named \"{mary.Children[0].Name}\"."); WriteLine($"5! is {Person.Factorial(5)}"); harry.Shout += Harry_Shout; harry.Poke(); harry.Poke(); harry.Poke(); harry.Poke(); Person[] people = { new Person { Name = "Simon" }, new Person { Name = "Jenny" }, new Person { Name = "Adam" }, new Person { Name = "Richard" } }; WriteLine("Initial list of people:"); foreach (var person in people) { WriteLine($"{person.Name}"); } WriteLine("Use Person's IComparable implementation to sort:"); Array.Sort(people); foreach (var person in people) { WriteLine($"{person.Name}"); } WriteLine("use PersonComparer's IComparer implementation to sort:"); Array.Sort(people, new PersonComparer()); foreach (var person in people) { WriteLine($"{person.Name}"); } var t = new Thing(); t.Data = 42; WriteLine($"Thing: {t.Process("42")}"); var gt = new GenericThing <int>(); gt.Data = 42; WriteLine($"GenericThing: {gt.Process("42")}"); string number1 = "4"; WriteLine($"{number1} squared is {Squarer.Square<string>(number1)}"); byte number2 = 3; WriteLine($"{number2} squared is {Squarer.Square<byte>(number2)}"); var dv1 = new DisplacementVector(3, 5); var dv2 = new DisplacementVector(-2, 7); var dv3 = dv1 + dv2; WriteLine($"({dv1.X}, {dv1.Y}) + ({dv2.X}, {dv2.Y}) = ({dv3.X}, {dv3.Y})"); Employee e1 = new Employee { Name = "John Jones", DateOfBirth = new DateTime(1990, 7, 28) }; e1.WriteToConsole(); e1.EmployeeCode = "JJ001"; e1.HireDate = new DateTime(2014, 11, 23); WriteLine($"{e1.Name} was hired on {e1.HireDate:dd/MM/yy}"); WriteLine(e1.ToString()); Employee aliceInEmployee = new Employee { Name = "Alice", EmployeeCode = "AA123" }; Person aliceInPerson = aliceInEmployee; aliceInEmployee.WriteToConsole(); aliceInPerson.WriteToConsole(); WriteLine(aliceInEmployee.ToString()); WriteLine(aliceInPerson.ToString()); if (aliceInPerson is Employee) { WriteLine($"{nameof(aliceInPerson)} IS an Employee"); Employee e2 = (Employee)aliceInPerson; // do something with e2 } Employee e3 = aliceInPerson as Employee; if (e3 != null) { WriteLine($"{nameof(aliceInPerson)} AS an Employee"); // do something with e3 } try { e1.TimeTravel(new DateTime(1999, 12, 31)); e1.TimeTravel(new DateTime(1950, 12, 25)); } catch (PersonException ex) { WriteLine(ex.Message); } string email1 = "*****@*****.**"; string email2 = "ian&test.com"; WriteLine($"{email1} is a valid e-mail address: {email1.isValidEmail()}."); WriteLine($"{email2} is a valid e-mail address: {email2.isValidEmail()}."); }
static void Main(string[] args) { //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 p1 = new Person(); p1.Name = "Bob Smith"; p1.DateOfBirth = new System.DateTime(1965, 12, 22); WriteLine($"{p1.Name} was born on {p1.DateOfBirth:dddd, d MMMM yyyy}"); var p2 = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 17) }; WriteLine($"{p2.Name} was born on {p2.DateOfBirth:d MMMM yy}"); p1.FavouriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine($"{p1.Name}'s favourite wonder is {p1.FavouriteAncientWonder}"); p1.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // p1.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{p1.Name}'s bucket list is {p1.BucketList}"); p1.Children.Add(new Person { Name = "Alfred" }); p1.Children.Add(new Person { Name = "Zoe" }); WriteLine($" {p1.Name} has {p1.Children.Count} children:"); for (int child = 0; child < p1.Children.Count; child++) { WriteLine($" {p1.Children[child].Name}"); } BankAccount.InterestRate = 0.012M; var ba1 = new BankAccount(); ba1.AccountName = "Mrs. Jones"; ba1.Balance = 2400; WriteLine($"{ba1.AccountName} earned {ba1.Balance * BankAccount.InterestRate:C} interest."); var ba2 = new BankAccount(); ba2.AccountName = "Ms. Gerrier"; ba2.Balance = 98; WriteLine($"{ba2.AccountName} earned {ba2.Balance * BankAccount.InterestRate:C} interest."); WriteLine($"{p1.Name} is a {Person.Species}"); var p3 = new Person(); WriteLine($"{p3.Name} was instantiated at {p3.Instantiated:hh:mm:ss} on {p3.Instantiated:dddd, d MMMM yyyy}"); var p4 = new Person("Aziz"); WriteLine($"{p4.Name} was instantiated at { p4.Instantiated:hh:mm:ss} on { p4.Instantiated:dddd, d MMMM yyyy}"); p1.WriteToConsole(); WriteLine(p1.GetOrigin()); Tuple <string, int> fruit4 = p1.GetFruitCS4(); WriteLine($"There are {fruit4.Item2} {fruit4.Item1}."); (string, int)fruit7 = p1.GetFruitCS7(); WriteLine($"{fruit7.Item1}, {fruit7.Item2} there are."); var fruitNamed = p1.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} {fruitNamed.Name}?"); // C# 7.0 var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (p1.Name, p1.Children.Count); WriteLine($"{thing2.Item1} has {thing2.Item2} children."); // C# 7.1 var thing3 = (p1.Name, p1.Children.Count); WriteLine($"{thing3.Name} has {thing3.Count} children."); // деконструкция кортежей (string fruitName, int fruitNumber) = p1.GetFruitCS7(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(p1.SayHello()); WriteLine(p1.SayHello("Emily")); WriteLine(p1.OptionalParameters()); WriteLine(p1.OptionalParameters("Jump!", 98.5)); WriteLine(p1.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(p1.OptionalParameters("Poke!", active: false)); int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); p1.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); // упрощенный синтаксис параметров out в C# 7 int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); p1.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavoriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "Red"; WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); sam.Children.Add(new Person { Name = "Charlie" }); sam.Children.Add(new Person { Name = "Ella" }); WriteLine($"Sam's first child is {sam.Children[0].Name}"); WriteLine($"Sam's second child is {sam.Children[1].Name}"); WriteLine($"Sam's first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { // 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 p1 = new Person(); p1.Name = "Bob Smith"; p1.DateOfBirth = new System.DateTime(1965, 12, 22); p1.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.TempleOfArtemisAtEphesus; //WriteLine ( $"{p1.Name} was born on {p1.DateOfBirth: dddd, d MMMM yyyy}" ); WriteLine($"{p1.Name}'s favourite wonder is {p1.BucketList}"); WriteLine($"{p1.Name} is a {Person.Species}."); //WriteLine($"{p1.Name} was born on {p1.HomePlanet}"); p1.WriteToConsole(); WriteLine(p1.GetOrigin()); WriteLine(p1.SayHello()); WriteLine(p1.SayHello("Emily")); WriteLine(p1.OptionalParameters("Jump!", 98.5)); p1.Children.Add(new Person { Name = "Alfred" }); p1.Children.Add(new Person { Name = "Zoe" }); WriteLine($"{p1.Name} has {p1.Children.Count} children:"); // for ( int child = 0; child < p1.Children.Count; child++ ) // { // WriteLine($" {p1.Children[child].Name}"); // } foreach (var child in p1.Children) { WriteLine($" {child.Name}"); } BankAccount.InterestRate = 0.012M; var ba1 = new BankAccount(); ba1.AccountName = "Mrs. Jones"; ba1.Balance = 2400; WriteLine($"{ba1.AccountName} earned {ba1.Balance * BankAccount.InterestRate:C} interest."); var ba2 = new BankAccount(); ba2.AccountName = "Ms. Gerrier"; ba2.Balance = 98; WriteLine($"{ba2.AccountName} earned {ba2.Balance * BankAccount.InterestRate:C} interest."); var p3 = new Person(); WriteLine($"{p3.Name} was instantiated at {p3.Instantiated:h:mm:ss tt zz} on {p3.Instantiated:D}"); var p4 = new Person("Aziz"); WriteLine($"{p4.Name} was instantiated at {p4.Instantiated:h:mm:ss tt} on {p4.Instantiated:D}"); // Tuples WriteLine("\nUsing Tuples..."); Tuple <string, int> fruit4 = p1.GetFruitCS4(); WriteLine($"There are {fruit4.Item1} {fruit4.Item2}."); // (string, int) fruit7 = p1.GetFruitCS7 (); // WriteLine($"{fruit7.Name}, {fruit7.Number} there are."); var fruitNamed = p1.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} {fruitNamed.Name}?"); // Tuple name inference WriteLine("\nUsing Tuple name inferencing..."); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (p1.Name, p1.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); // Deconstructing a Tuple for its individual values. WriteLine("\nDeconstructing a tuple..."); (string fruitName, int fruitNumber) = p1.GetFruitCS7(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); // Passing parameters into functions int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); p1.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); p1.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavouriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favourite ice-cream flavor is {sam.FavouriteIceCream}."); sam.FavouritePrimaryColor = "Red"; WriteLine($"Sam's favourite primary color is {sam.FavouritePrimaryColor}."); sam.Children.Add(new Person { Name = "Charlie" }); sam.Children.Add(new Person { Name = "Ella" }); WriteLine($"Sam's first child is {sam.Children [ 0 ].Name}"); WriteLine($"Sam's second child is {sam.Children [ 1 ].Name}"); WriteLine($"Sam's first child is {sam [ 0 ].Name}"); WriteLine($"Sam's second child is {sam [ 1 ].Name}"); }
static void Main(string[] args) { // var person1 = new Person(); // person1.Name = "Juliani"; // person1.DateOfBirth = new System.DateTime(1995, 12, 12); // person1.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; // person1.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon |WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // WriteLine($"{person1.Name} was born on {person1.DateOfBirth :dddd, d MMMM yyyy} and {person1.Name}'s favorite wonder is {person1.FavoriteAncientWonder}"); // // p1.BucketList = (WondersOfTheAncientWorld)18; // WriteLine($"{person1.Name}'s bucket list is {person1.BucketList}"); var person2 = new Person { Name = "Janete", DateOfBirth = new DateTime(1977, 8, 19) }; WriteLine($"{person2.Name} was born on {person2.DateOfBirth :d MMM yy} and is a {Person.Species} from {person2.HomePlanet}"); person2.Children.Add(new Person { Name = "Juliani" }); person2.Children.Add(new Person { Name = "Julia" }); WriteLine( $"{person2.Name} has {person2.Children.Count} children:"); for (int child = 0; child < person2.Children.Count; child++) { WriteLine($" {person2.Children[child].Name}"); } BankAccount.InterestRate = 0.012M; var ba1 = new BankAccount(); ba1.AccountName = "Mrs. Jones"; ba1.Balance = 2400; WriteLine($"{ba1.AccountName} earned {ba1.Balance * BankAccount.InterestRate:C} interest."); var ba2 = new BankAccount(); ba2.AccountName = "Ms. Gerrier"; ba2.Balance = 98; WriteLine($"{ba2.AccountName} earned {ba2.Balance * BankAccount.InterestRate:C} interest."); var p3 = new Person(); WriteLine($"{p3.Name} was instantiated at {p3.Instantiated:hh:mm:ss} on {p3.Instantiated:dddd, d MMMM yyyy}"); var p4 = new Person("Aziz"); WriteLine($"{p4.Name} was instantiated at {p4.Instantiated:hh:mm:ss} on {p4.Instantiated:dddd, d MMMM yyyy}"); p4.WriteToConsole(); WriteLine(p4.GetOrigin()); Tuple <string, int> fruit4 = p4.GetFruitCS4(); WriteLine($"There are {fruit4.Item2} {fruit4.Item1}."); (string, int)fruit7 = p4.GetFruitCS7(); WriteLine($"{fruit7.Item1}, {fruit7.Item2} there are."); var fruitNamed = p4.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} {fruitNamed.Name}?"); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (person2.Name, person2.Children.Count); WriteLine($"{thing2.Item1} has {thing2.Item2} children."); (string fruitName, int fruitNumber) = person2.GetFruitCS7(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(person2.SayHello()); WriteLine(person2.SayHelloTo("Emily")); int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); person2.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); // simplified C# 7 syntax for out parameters int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); person2.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); }
static void Main(string[] args) { var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); 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 Chapter05() { var p1 = new Person(); p1.Name = "Bob Smith"; p1.DateOfBirth = new DateTime(1965, 12, 22); WriteLine($"{p1.Name} was born on {p1.DateOfBirth:dddd, d MMMM yyyy}"); var p2 = new Person() { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 17) }; WriteLine($"{p2.Name} was born on {p2.DateOfBirth:d MMM yy}"); // without flags - one only p1.FavouriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine($"{p1.Name}'s favourite wonder is {p1.FavouriteAncientWonder}"); // p1.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | // WondersOfTheAncientWorld.MausoleumAtHalicarnassus; p1.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{p1.Name}'s bucket list is {p1.BucketList}"); // Add people to chlidren list p1.Children.Add(new Person("Alfred")); p1.Children.Add(new Person("Zoe")); WriteLine($"{p1.Name} has {p1.Children.Count} children:"); for (int child = 0; child < p1.Children.Count; child++) { WriteLine($" {p1.Children[child].Name}"); } BankAccount.InterestRate = 0.012M; var ba1 = new BankAccount(); ba1.AccountName = "Mrs. Jones"; ba1.Balance = 2400; WriteLine($"{ba1.AccountName} earned {ba1.Balance * BankAccount.InterestRate:C} Interest."); var ba2 = new BankAccount { AccountName = "Ms. Gerrier", Balance = 98 }; WriteLine($"{ba2.AccountName} earned {ba2.Balance * BankAccount.InterestRate:C} Interest."); WriteLine($"{p1.Name} is a {Person.Species}"); var p3 = new Person(); WriteLine($"{p3.Name} was instantiated at {p3.Instantiated:hh:mm:ss} on {p3.Instantiated:dddd, d MMMM yyyy}"); var p4 = new Person("Aziz"); WriteLine($"{p4.Name} was instantiated at {p4.Instantiated:hh:mm:ss} on {p4.Instantiated:dddd, d MMMM yyyy}"); p1.WriteToConsole(); WriteLine(p1.GetOrigin()); Tuple <string, int> fruit4 = p1.GetFruitCS4(); WriteLine($"There are {fruit4.Item2} {fruit4.Item1}."); (string, int)fruit7 = p1.GetFruitCS7(); WriteLine($"{fruit7.Item1}, {fruit7.Item2} there are."); var fruitNamed = p1.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} {fruitNamed.Name}?"); // Inferring tuple names var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children"); var thing2 = (p1.Name, p1.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children"); // YOu can deconstruct Tuples (string fruitName, int fruitNumber) = p1.GetFruitCS7(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); // say hello WriteLine(p1.SayHello()); WriteLine(p1.SayHello("Emily")); // optional parameters WriteLine(p1.OptionalParameters()); // pass parameters in order WriteLine(p1.OptionalParameters("Jump!", 98.5)); // use named parameters to negate requirement for order WriteLine(p1.OptionalParameters(number: 52.7, command: "Hide!")); // skip paramters using named arguments WriteLine(p1.OptionalParameters(command: "Poke!", active: false)); // passing parameters int a = 10; int b = 20; // value a is recreated in method and not returned. // reference to b is passed - which is why it can change // c is created in the method and so anything passed in is lost. WriteLine($"Before: a = {a}, b = {b}, c isn't created yet!"); p1.PassingParameters(a, ref b, out int c); WriteLine($"Before: a = {a}, b = {b}, c = {c}"); // Using partial Person class var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); // using getters and setters sam.FavouriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favourite ice-cream flavour is {sam.FavouriteIceCream}."); sam.FavouritePrimaryColor = "Red"; WriteLine($"Sam's favourite primary color is {sam.FavouritePrimaryColor}."); try { sam.FavouritePrimaryColor = "Yellow"; WriteLine($"Sam's favourite primary color is {sam.FavouritePrimaryColor}."); } catch (Exception ex) { WriteLine($"{ex.GetType()}: {ex.Message}"); } }
static void Main(string[] args) { Person bob = new Person(); WriteLine(bob.ToString()); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 2); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine( format: "{0} was born on {1:dddd, dd/MM/yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); WriteLine(format: "{0}'s favorite wonder is {1}. Its integer is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder); bob.Children.Add(new Person()); bob.Children.Add(new Person { Name = "Zoe" }); WriteLine( $"{bob.Name} has {bob.Children.Count} children:"); for (int child = 0; child < bob.Children.Count; child++) { WriteLine($" {bob.Children[child].Name}"); } WriteLine($"{bob.Name}'s 1st child is {bob.Children[0].Name}"); // indexer WriteLine($"{bob.Name}'s 1st child is {bob[0].Name}"); // const field WriteLine( $"{bob.Name} is a {Person.Species}"); // Add Friend bob.addFriend(new Person("Edward", "Earth")); bob.addFriend(new Person("Edwin", "Mars")); List<Person> allFriends = bob.getFriend(); for(int idx=0; idx< allFriends.Count; idx++){ WriteLine($"{bob.Name}'s friend {idx}: {allFriends[idx].Name}"); } for(int idx=0; idx< allFriends.Count; idx++){ string idxString = idx.ToString(); WriteLine($"{bob.Name}'s friend {idx}: {bob[idxString].Name}"); } // calling Method WriteLine("** WriteLine(bob.GetOrigin())"); WriteLine(bob.GetOrigin()); WriteLine("** bob.WriteToConsole()"); bob.WriteToConsole(); var fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = bob.GetNamedFruit(); WriteLine($"{fruitNamed.Name}, {fruitNamed.Number} there are."); (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); (string fruitName, int fruitNumber) dcFruit = bob.GetFruit(); WriteLine($"Deconstructed 2: {dcFruit.fruitName}, {dcFruit.fruitNumber}"); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Edward")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); WriteLine(bob.OptionalParameters(command: "ALL!", active: false)); int d = 10; int e = 20; WriteLine( $"Before: d = {d}, e = {e}, f doesn't exist yet!"); // simplified C# 7.0 syntax for the out parameter bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine( format: "{0} was born on {1:dddd, dd/MM/yyyy}", arg0: alice.Name, arg1: alice.DateOfBirth); var blankPerson = new Person(); WriteLine(format:"{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", arg0: blankPerson.Name, arg1: blankPerson.HomePlanet, arg2: blankPerson.Instantiated); var gunny = new Person(initialName:"Gunny", homePlanet:"Mars"); WriteLine( format:"{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", arg0: gunny.Name, arg1: gunny.HomePlanet, arg2: gunny.Instantiated); // Things of Default var default1 = new ThingOfDefaults(); WriteLine("** Thing of Default"); WriteLine( format: "int: {0}, DateTime:{1}, string:{2}, {3},", default1.Population, default1.When, default1.Name, default1.People.Count ); // ---------------------- // BankAccount // ---------------------- BankAccount.InterestRate = 0.012M; // store a shared value var jonesAccount = new BankAccount { AccountName= "Mrs. Jones", Balance= 2400 }; WriteLine(format: "{0}'s Balanace is {1:N} earned {2:C} interest.", arg0: jonesAccount.AccountName, arg1: jonesAccount.Balance, arg2: jonesAccount.Balance * BankAccount.InterestRate); var gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; WriteLine(format: "{0}'s Balanace is {1:N} earned {2:C} interest.", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance, arg2: gerrierAccount.Balance * BankAccount.InterestRate); // Try Property var sam = new Person(initialName:"Sam", homePlanet:"Mars", initBirth: new DateTime(1972, 1, 27)); WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavoriteIceCream = "Stars"; WriteLine($"{sam.Name}'s favorite icecream is {sam.FavoriteIceCream}"); sam.FavoritePrimaryColor="red"; WriteLine($"{sam.Name}'s favorite Color is {sam.FavoritePrimaryColor}"); try{ sam.FavoritePrimaryColor="yellow"; } catch (ArgumentException argEx){ WriteLine($"{argEx.GetType()} says {argEx.Message}"); } // -------------------------------------- // Flight Patterns(object pattern matching) // -------------------------------------- WriteLine("// -------------------------------"); WriteLine("//Flight Patterns(object pattern matching"); WriteLine("// -------------------------------"); var passengers = new object[5]{ new FirstClassPassenger { AirMiles = 1_419 },
static void Main(string[] args) { var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); WriteLine( format: "{0} was born on {1:dddd, d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth); // WriteLine($"{bob.Name} was born on {bob.DateOfBirth:dddd, d MMMM yyyy}"); var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine( format: "{0} was born on {1:dd MMM y}", arg0: alice.Name, arg1: alice.DateOfBirth); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine( format: "{0}'s favorite wonder is {1}. Its integer is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder); bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // other way to write this is below--don't use it b/c it's harder to read // bob.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{bob.Name}'s bucket list is {bob.BucketList}"); bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Zoe" }); WriteLine( $"{bob.Name} has {bob.Children.Count} children: "); for (int child = 0; child < bob.Children.Count; child++) { WriteLine($" {bob.Children[child].Name}"); } // foreach (Person child in bob.Children) // { // // WriteLine($" {child.Name}"); // Write($" {child.Name} "); // } // WriteLine(); BankAccount.InterestRate = 0.012M; // store a shared value var jonesAccount = new BankAccount(); jonesAccount.AccountName = "Mrs. Jones"; jonesAccount.Balance = 2400; WriteLine(format: "{0} earned {1:C} interest.", arg0: jonesAccount.AccountName, arg1: jonesAccount.Balance * BankAccount.InterestRate); var gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; WriteLine(format: "{0} earned {1:C} interest.", arg0: gerrierAccount.AccountName, arg1: gerrierAccount.Balance * BankAccount.InterestRate); WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); var blankPerson = new Person(); WriteLine(format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", arg0: blankPerson.Name, arg1: blankPerson.HomePlanet, arg2: blankPerson.Instantiated); var gunny = new Person("Gunny", "Mars"); WriteLine(format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", arg0: gunny.Name, arg1: gunny.HomePlanet, arg2: gunny.Instantiated); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = bob.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}."); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); WriteLine(bob.OptionalParameters( number: 52.7, command: "Hide!")); WriteLine(bob.OptionalParameters("Poke!", active: false)); int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); bob.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); int d = 10; int e = 20; WriteLine( $"Before: d = {d}, e = {e}, f doesn't exist yet!"); //simplified C# 7.0 syntax for the out parameter bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavoriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "Red"; WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); sam.Children.Add(new Person { Name = "Charlie" }); sam.Children.Add(new Person { Name = "Ella" }); WriteLine($"Sam's first child is {sam.Children[0].Name}"); WriteLine($"Sam's second child is {sam.Children[1].Name}"); WriteLine($"Sam's first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); object[] passengers = { new FirstClassPassenger { AirMiles = 1_419 },
static void Main(string[] args) { var p1 = new Person { Name = "Bob Smith", DateOfBirth = new System.DateTime(1965, 12, 22), BucketList = WondersOfTheAncientWorld.StatueOfZeusAtOlympia }; WriteLine($"{p1.Name} was born on {p1.DateOfBirth:dddd, d MMMM yyyy}"); WriteLine($"{p1.Name}'s favurite wonder is {p1.BucketList}"); p1.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; //p1.BucketList = (WondersOfTheAncientWorld)18; WriteLine($"{p1.Name}'s bucket list is {p1.BucketList}"); p1.Children.Add(new Person { Name = "Alfred" }); p1.Children.Add(new Person { Name = "Zoe" }); WriteLine($"{p1.Name} has {p1.Children.Count} children:"); for (int child = 0; child < p1.Children.Count; child++) { WriteLine($"{p1.Children[child].Name}"); } WriteLine($"{p1.Name} is {Person.Species}"); WriteLine($"{p1.Name} was born on {p1.HomePlanet}"); var p2 = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 17) }; WriteLine($"{p2.Name} was born on {p2.DateOfBirth:d MMM yy}"); WriteLine(new string('-', 50)); var p3 = new Person(); WriteLine($"{p3.Name} was instantiated at {p3.Instantiated:hh:mm:ss} on " + $"{p3.Instantiated:dddd, d MMMM yyyy}"); var p4 = new Person("Aziz"); WriteLine($"{p4.Name} was instantiated at {p4.Instantiated:hh:mm:ss} on " + $"{p4.Instantiated:dddd, d MMMM yyyy}"); WriteLine(new string('-', 50)); p1.WriteToConsole(); WriteLine(p1.GetOrigin()); WriteLine(new string('-', 50)); BankAccount.InterestRate = 0.012M; var ba1 = new BankAccount { AccountName = "Mrs. Jones", Balance = 2400 }; WriteLine($"{ba1.AccountName} earned {ba1.Balance * BankAccount.InterestRate:C} interest"); var ba2 = new BankAccount { AccountName = "Ms.Gerrier", Balance = 98 }; WriteLine($"{ba2.AccountName} earned {ba2.Balance * BankAccount.InterestRate:C} interest"); WriteLine(new string('-', 50)); // Tuples Tuple <string, int> fruit4 = p1.GetFruitCS4(); WriteLine($"There are {fruit4.Item2} {fruit4.Item1}"); (string, int)fruit7 = p1.GetFruitCS7(); WriteLine($"{fruit7.Item1}, {fruit7.Item2} there are"); var fruitNamed = p1.GetNamedFruit(); WriteLine($"Are there {fruitNamed.Number} {fruitNamed.Name}?"); WriteLine(new string('-', 50)); var thing1 = ("Neville", 4); WriteLine( $"{thing1.Item1} has {thing1.Item2} children"); var thing2 = (p1.Name, p1.Children.Count); WriteLine( $"{thing2.Name} has {thing2.Count} children"); WriteLine(new string('-', 50)); WriteLine(p1.SayHello()); WriteLine(p1.SayHello("Emily")); WriteLine(new string('-', 50)); WriteLine(p1.OptionalParameters()); WriteLine(p1.OptionalParameters("Jump!", 98.5)); WriteLine(p1.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(new string('-', 50)); int a = 10, b = 20, c = 30; WriteLine($"Before : a = {a}, b = {b}, c = {c}"); p1.PassingParametrs(a, ref b, out c); WriteLine($"After : a = {a}, b = {b}, c = {c}"); // simplify out parameters syntax in C# 7 int d = 10; int e = 20; WriteLine($"Before : d = {d}, e = {e}, f doesn't exist yet"); p1.PassingParametrs(d, ref e, out int f); WriteLine($"Before : d = {d}, e = {e}, f = {f}"); WriteLine(new string('-', 50)); var Sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(Sam.Origin); WriteLine(Sam.Greeting); Write(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}."); WriteLine(new string('-', 50)); 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 swcond child is {Sam.Children[1].Name}"); WriteLine($"Sam's first child is {Sam[0].Name}"); WriteLine($"Sam's second child is {Sam[1].Name}"); }
static void Main(string[] args) { var bob = new Person(); // WriteLine(bob.ToString()); 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(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) { var john = new Person("John", "Earth"); john.Name = "John Doe"; john.DateOfBirth = new DateTime(1992, 6, 19); WriteLine(format: "{0} was born on {1:dddd, d MMMM yyyy}", // dddd: name of day, d: number of day, MMMM: name of month, yyyy: full year arg0: john.Name, arg1: john.DateOfBirth); WriteLine(format: "{0} was born on {1:dd MMM yy}", arg0: john.Name, arg1: john.DateOfBirth); WriteLine(); // ==========Enums========== // Using enums in PacktLibrary WondersOfTheAncientWorld.cs john.FavoriteWonder = WondersOfTheAncientWorld.HangingGardensOfBabylon; WriteLine(format: "{0}'s favorite wonder is {1}. It's integer is {2}", arg0: john.Name, arg1: john.FavoriteWonder, arg2: (int)john.FavoriteWonder); // enum valuesa are initialy stored as an int for efficiency. // enums can also be called and set using int values when useing the decoration [System.Flags] and inheriting from byte john.BucketList = WondersOfTheAncientWorld.ColossusOfRhodes | WondersOfTheAncientWorld.GreatPyramidOfGiza; WriteLine($"{john.Name}'s bucket list is {john.BucketList}"); john.BucketList = (WondersOfTheAncientWorld)33; WriteLine($"{john.Name}'s bucket list is {john.BucketList}"); WriteLine(); // ==========Objects with Lists========== // calling from the public List<Person> in PacktLibrary Person.cs john.Children.Add(new Person("Jane", john.HomePlanet)); john.Children.Add(new Person("Jack", john.HomePlanet)); WriteLine($"{john.Name} has {john.Children.Count} children:"); for (int child = 0; child < john.Children.Count; child++) { WriteLine($" {john.Children[child].Name}"); } WriteLine(); // ==========Using static values========== // using BankAccount.cs class BankAccount.InterestRate = 0.12M; // store a shared value, InterestRate is static var doeAccount = new BankAccount(); doeAccount.AccountName = "Mr. Doe"; doeAccount.Balance = 43_600; WriteLine(format: "{0} earned {1:C} interest.", // :C - currency format arg0: doeAccount.AccountName, arg1: doeAccount.Balance * BankAccount.InterestRate); var smithAccount = new BankAccount(); smithAccount.AccountName = "Mr. Smith"; smithAccount.Balance = 1_400; WriteLine(format: "{0} earned {1:C} interest.", // :C - currency format arg0: smithAccount.AccountName, arg1: smithAccount.Balance * BankAccount.InterestRate); // using const value from Person.cs WriteLine($"{john.Name} is a {Person.Species}. (We hope)"); // using readonly value from Person.cs WriteLine($"{john.Name} is from {john.HomePlanet}. (If he is a H**o Sapien)"); WriteLine(); // ==========Contrcutors========== // Contrcutors with arguments // using PersonArgs.cs var mike = new Person("Mike", "Mars"); WriteLine(format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", arg0: mike.Name, arg1: mike.HomePlanet, arg2: mike.Instantiated); // using methods mike.WriteToConsole(); WriteLine(mike.GetOrigin()); WriteLine(); // ==========Tuples========== // using tuples (string, int)fruit = mike.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = mike.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}."); // variable use of tuples var thing1 = ("George", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} Children."); var thing2 = (john.Name, john.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); // Deconstructing tuples (string fruitName, int fruitNumber) = mike.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); // taking the values held in GetFruit() and assigning the tuple to the new variables WriteLine(); // ==========Param Methods and Overloading========== WriteLine(mike.SayHello()); WriteLine(mike.SayHello("John")); WriteLine(); // ==========Optional Parameters========== WriteLine(mike.OptionalParameters()); // will output default values in PersonArgs.cs WriteLine(mike.OptionalParameters("Jump!", 98.5)); // overwrites the default values // delcaring some parameters can be skipped if you name the parameters based on the method WriteLine(mike.OptionalParameters(number: 29.1, command: "Hide!")); // parameter calues can be named when declaring them WriteLine(mike.OptionalParameters("Walk!", active: false)); WriteLine(); // ==========Controlling How Parameters are Passed========== // by value (default)(in), ref(in-out), and as an out parameter(out) int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}."); mike.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}."); // When passing a value as a parameter by default, the current VALUE // is passed, not the variable itself. a before and after is 10. // When passing y as a reference to b, b increments as y increments. // When using an out, the value in z overwrites the value of c. // In C# 7.0 we can simpify code that uses the out variable: int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); // simplified syntax for the out parameter mike.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f ={f}"); WriteLine(); // ==========Using Properties in C# 6+========== // Using PersonAutoGen.cs var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1928, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); // settable properties sam.FavoriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favorite ice cream is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "red"; WriteLine($"Sam's favorite color is {sam.FavoritePrimaryColor}."); // ==========Indexers========== sam.Children.Add(new Person { Name = "Charlie" }); sam.Children.Add(new Person { Name = "Ella" }); WriteLine($"Sam's first child is {sam.Children[0].Name}"); WriteLine($"Sam's second child is {sam.Children[1].Name}"); WriteLine($"Sam's first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { var edixon = new Person(); edixon.Name = "Edixon van Vliet"; edixon.DateOfBirth = new DateTime(1996, 3, 27); WriteLine( "{0} was born on {1:dddd, d MMMM yyyy}", edixon.Name, edixon.DateOfBirth ); edixon.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; WriteLine( "{0}'s favorite wonder is {1}. It's integer is {2}.", edixon.Name, edixon.FavoriteAncientWonder, (int)edixon.FavoriteAncientWonder ); edixon.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; WriteLine($"{edixon.Name}'s bucket list is {edixon.BucketList}"); edixon.Children.Add(new Person { Name = "Alfred" }); edixon.Children.Add(new Person { Name = "Zoe" }); WriteLine($"{edixon.Name} has {edixon.Children.Count} children:"); foreach (Person child in edixon.Children) { WriteLine($" {child.Name}"); } BankAccount.InterestRate = 0.012m; var jonesAccount = new BankAccount(); jonesAccount.AccountName = "Mrs. Jones"; jonesAccount.Balance = 2400m; WriteLine( "{0} earned {1:c} interest.", jonesAccount.AccountName, jonesAccount.Balance * BankAccount.InterestRate ); var gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98m; WriteLine( "{0} earned {1:c} interest.", gerrierAccount.AccountName, gerrierAccount.Balance * BankAccount.InterestRate ); WriteLine($"{edixon.Name} is a {Person.Species}"); WriteLine($"{edixon.Name} was born on {edixon.HomePlanet}"); var blankPerson = new Person(); WriteLine(format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", blankPerson.Name, blankPerson.HomePlanet, blankPerson.Instantiated ); var gunny = new Person("Gunny", "Mars"); WriteLine( "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", gunny.Name, gunny.HomePlanet, gunny.Instantiated ); edixon.WriteToConsole(); WriteLine(edixon.GetOrigin()); (string, int)fruit = edixon.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var fruitNamed = edixon.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}"); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (edixon.Name, edixon.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); (string fruitName, int fruitNumber) = edixon.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(edixon.SayHello()); WriteLine(edixon.SayHello("Emily")); WriteLine(edixon.OptionalParameters()); WriteLine(edixon.OptionalParameters("Jump!", 98.5)); WriteLine(edixon.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(edixon.OptionalParameters("Poke!", active: false)); int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); edixon.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); int d = 10; int e = 20; WriteLine( $"Before: d = {d}, e = {e}, f doesn't exist yet!"); // simplified C# 7.0 syntax for the out parameter edixon.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavoriteIceCream = "Chocolate Fudge"; WriteLine( $"{sam.Name}'s favorite ice-cream flavor is {sam.FavoriteIceCream}." ); sam.FavoritePrimaryColor = "Red"; WriteLine( $"{sam.Name}'s favorite primary color is {sam.FavoritePrimaryColor}." ); sam.Children.Add(new Person { Name = "Charlie" }); sam.Children.Add(new Person { Name = "Ella" }); WriteLine($"Sam's first child is {sam.Children[0].Name}"); WriteLine($"Sam's second child is {sam.Children[1].Name}"); WriteLine($"Sam's first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1954, 12, 22); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; bob.BucketList = (WondersOfTheAncientWorld)18; bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Suzie" }); WriteLine( format: "{0} was born on {1:dddd, d MMMM yyyy}", arg0: bob.Name, arg1: bob.DateOfBirth ); WriteLine( format: "{0}'s favorite wonder is {1}. It's integer is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder ); WriteLine($"{bob.Name}'s bucket list is {bob.BucketList}"); WriteLine($"Bob has {bob.Children.Count} children."); for (int child = 0; child < bob.Children.Count; child++) { WriteLine($"{bob.Children[child].Name}"); } ; foreach (Person child in bob.Children) { WriteLine($"Saying hello to {child.Name}!"); } ; WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine( format: "{0} was born on {1:dd MMM yy}", arg0: alice.Name, arg1: alice.DateOfBirth ); // Bank Accounts BankAccount.InterestRate = 0.012M; // storing a shared value var jonesAccount = new BankAccount(); jonesAccount.AccountName = "Mrs. Jones"; jonesAccount.Balance = 2400; WriteLine(format: "{0} earned {1:C} interest.", arg0: jonesAccount.AccountName, arg1: jonesAccount.Balance * BankAccount.InterestRate ); var leslieAccount = new BankAccount(); leslieAccount.AccountName = "Mr. Alldridge"; leslieAccount.Balance = 100; WriteLine(format: "{0} earned {1:C} interest.", arg0: leslieAccount.AccountName, arg1: leslieAccount.Balance * BankAccount.InterestRate ); var blankPerson = new Person(); WriteLine( format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", arg0: blankPerson.Name, arg1: blankPerson.HomePlanet, arg2: blankPerson.Instantiated ); var gunny = new Person("Gunny", "Mars"); WriteLine( format: "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.", arg0: gunny.Name, arg1: gunny.HomePlanet, arg2: gunny.Instantiated ); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); var namedFruit = bob.GetNamedFruit(); WriteLine($"{namedFruit.Name} and {namedFruit.Number} exist"); var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); var thing2 = (bob.Name, bob.Children.Count); WriteLine($"{thing2.Name} has {thing2.Count} children."); // infers Name and Count as the named fields // deconstructing tuples (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed {fruitName}, {fruitNumber}"); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Leslie")); WriteLine(bob.OptionalParams()); WriteLine(bob.OptionalParams(command: "Jump", number: 99)); WriteLine(bob.OptionalParams("Poke!", active: false)); int a = 10; int b = 20; WriteLine($"Before: a = {a}, b = {b}, c = doesn't exist"); bob.PassingParameters(a, ref b, out int c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1992, 6, 9) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavouriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favourite ice-cream flavour is {sam.FavouriteIceCream}."); sam.FavouritePrimaryColor = "Red"; WriteLine($"Sam's favourite primary color is {sam.FavouritePrimaryColor}"); sam.Children.Add(new Person { Name = "Charlie" }); sam.Children.Add(new Person { Name = "Ella" }); WriteLine($"Sam's first child is {sam.Children[0].Name}"); WriteLine($"Sam's second child is {sam.Children[1].Name}"); WriteLine($"Sam's first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { //Bob is instantiated and then his fields are set later var bob = new Person(); bob.Name = "Bob Smith"; bob.DateOfBirth = new DateTime(1965, 12, 22); bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia; bob.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon | WondersOfTheAncientWorld.MausoleumAtHalicarnassus; // equivalent to bob.BucketList = (WondersOfTheAncientWorld)18 due to byte bob.Children.Add(new Person { Name = "Alfred" }); bob.Children.Add(new Person { Name = "Zoe" }); WriteLine( format: "{0} was born on {1:dddd, d MMMM yyyy}.", arg0: bob.Name, arg1: bob.DateOfBirth ); WriteLine( format: "{0}'s favorite wonder is {1}. Its integer is {2}.", arg0: bob.Name, arg1: bob.FavoriteAncientWonder, arg2: (int)bob.FavoriteAncientWonder ); WriteLine($"{bob.Name}'s bucket list is {bob.BucketList}"); WriteLine($"{bob.Name} has {bob.Children.Count} children:"); //using a for loop to get the name of the children /* for (int child = 0; child < bob.Children.Count; child++) { * WriteLine($" {bob.Children[child].Name}"); * } */ //using a foreach loop to iterate through the list foreach (Person child in bob.Children) { WriteLine($" {child.Name}"); } WriteLine($"{bob.Name} is a {Person.Species}"); WriteLine($"{bob.Name} was born on {bob.HomePlanet}"); //Alice is instantiated with the fields set at that time var alice = new Person { Name = "Alice Jones", DateOfBirth = new DateTime(1998, 3, 7) }; WriteLine( format: "{0} was born on {1:dd MMM yy}", arg0: alice.Name, arg1: alice.DateOfBirth ); BankAccount.InterestRate = 0.012M; //store a shared value var jonesAccount = new BankAccount(); jonesAccount.AccountName = "Mrs. Jones"; jonesAccount.Balance = 2400; decimal jonesInterest = jonesAccount.Balance * BankAccount.InterestRate; WriteLine($"{jonesAccount.AccountName} earned {jonesInterest:C} interest."); var gerrierAccount = new BankAccount(); gerrierAccount.AccountName = "Ms. Gerrier"; gerrierAccount.Balance = 98; decimal gerrierInterest = (gerrierAccount.Balance * BankAccount.InterestRate); WriteLine($"{gerrierAccount.AccountName} earned {gerrierInterest:C} interest."); var blankPerson = new Person(); WriteLine($"{blankPerson.Name} of {blankPerson.HomePlanet} was created at " + $"{blankPerson.Instantiated:hh:mm:ss} on a {blankPerson.Instantiated:dddd}."); var gunny = new Person("Gunny", "Mars"); WriteLine($"{gunny.Name} of {gunny.HomePlanet} was created at " + $"{gunny.Instantiated:hh:mm:ss} on a {gunny.Instantiated:dddd}."); bob.WriteToConsole(); WriteLine(bob.GetOrigin()); //item syntax (string, int)fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); //named syntax var fruitNamed = bob.GetNamedFruit(); WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}"); //using the item1, item2 syntax var thing1 = ("Neville", 4); WriteLine($"{thing1.Item1} has {thing1.Item2} children."); //set these equal to the class fields var thing2 = (bob.Name, bob.Children.Count); //can reference it using those automatically WriteLine($"{thing2.Name} has {thing2.Count} children."); (string fruitName, int fruitNumber) = bob.GetFruit(); WriteLine($"Deconstructed: {fruitName}, {fruitNumber}"); WriteLine(bob.SayHello()); WriteLine(bob.SayHello("Emily")); WriteLine(bob.OptionalParameters()); WriteLine(bob.OptionalParameters("Jump!", 98.5)); WriteLine(bob.OptionalParameters(number: 52.7, command: "Hide!")); WriteLine(bob.OptionalParameters("Poke!", active: false)); int a = 10; int b = 20; int c = 30; WriteLine($"Before: a = {a}, b = {b}, c = {c}"); bob.PassingParameters(a, ref b, out c); WriteLine($"After: a = {a}, b = {b}, c = {c}"); int d = 10; int e = 20; WriteLine($"Before: d = {d}, e = {e}, f doesn't exist yet!"); //simplified C# 7.0 syntax for the out parameter bob.PassingParameters(d, ref e, out int f); WriteLine($"After: d = {d}, e = {e}, f = {f}"); var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(1972, 1, 27) }; WriteLine(sam.Origin); WriteLine(sam.Greeting); WriteLine(sam.Age); sam.FavoriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "Red"; WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); sam.Children.Add(new Person { Name = "Charlie" }); sam.Children.Add(new Person { Name = "Ella" }); WriteLine($"Sam's first child is {sam.Children[0].Name}"); WriteLine($"Sam's second child is {sam.Children[1].Name}"); WriteLine($"Sam's first child is {sam[0].Name}"); WriteLine($"Sam's second child is {sam[1].Name}"); }
static void Main(string[] args) { var Bob = new Person(); 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}"); }