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}");
        }
Esempio n. 2
0
        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}");
        }
Esempio n. 3
0
        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()}.");
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            var p1 = new Person("");

            p1.Name                   = "Bob Smith";
            p1.DateOfBirth            = new System.DateTime(1954, 12, 22);
            p1.FavouriteAncientWonder = WondersOfTheAncientWorld.StatueOfZeusAtOlympia;
            WriteLine($"{p1.Name} was born on {p1.DateOfBirth:dddd, d MMMM yyyy}. And favourite wonder is {p1.FavouriteAncientWonder}");
            WriteLine(p1.ToString());
            p1.BucketList = WondersOfTheAncientWorld.HangingGardensOfBabylon |
                            WondersOfTheAncientWorld.MausoleumAtHalicarnassus;
            // p1.BucketList = (WondersOfTheAncientWorld)18;
            WriteLine($"{p1.Name}'s bucket list is {p1.BucketList}");

            //TODO это возов конструктора такой через {}?
            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} was instantiated at {p1.Instantiated:hh:mm:ss} on {p1.Instantiated:dddd, d MMMM yyyy}");

            /* ****BankAccount**** */

            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} was born on {p1.HomePlanet}");

            /* TUPLES */

            Tuple <string, int> fruit = p1.GetFruitCS4();

            WriteLine($"There are {fruit.Item2} {fruit.Item1}.");

            (string, int)fruit7 = p1.GetFruitCS7();
            WriteLine($"{fruit7.Item1}, {fruit7.Item2} there are.");

            /* TUPLE DESTRUCTURING */
            var thing1 = ("Neville", 4);

            (string name, int childNum) = thing1;
            WriteLine(
                $"{thing1.Item1} has {thing1.Item2} children.");
            WriteLine($"Deconstructed {name} and his {childNum} children");

            /* Optional Named Parameters */
            WriteLine(p1.OptionalParameters(number: 52.7, command: "Hide!"));

            /* Управление передачей параметров */
            //TODO ref, out
            int a = 10;
            int b = 20;
            int c = 30;

            WriteLine($"Before: a = {a}, b = {b}, c = {c}");
            // p1.PassingParameters(a, ref b, out c);

            // System.Int32.TryParse("23", out int result);

            WriteLine($"After: a = {a}, b = {b}, c = {c}");

            /* OBJECT DUMPER */
            ObjectHelper.Dump(p1);

            /* Свойства только для чтения*/
            //TODO а какже readonly?

            var sam = new Person("")
            {
                Name        = "Sam",
                DateOfBirth = new DateTime(1972, 1, 27)
            };

            //TODO данные свойства внутри выполнряются как функции?
            WriteLine(sam.Origin);
            WriteLine(sam.Greeting);
            WriteLine(sam.Age);

            // Lambda Expressions
            Func <int, int, int> adder = (m, n) => m + n;

            //TODO Print out object properties.
            ObjectHelper.Dump(sam);

            // Computed 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}.");

            //ИНДЕКСАТОР
            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}");
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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}");
            }
        }
Esempio n. 8
0
        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}");
        }