static void Main(string[] args)
        {
            var harry = new Person("Harry");
            var mary  = new Person("Mary");
            var jill  = new Person("Jill");

            var baby1 = mary.ProcreateWith(harry);
            var baby2 = Person.Procreate(harry, jill);
            // multiply operator overloaded with the Person class:
            var baby3 = harry * mary;

            WriteLine($"{harry.Name} has {harry.Children.Count} children.");
            WriteLine($"{mary.Name} has {mary.Children.Count} children.");
            WriteLine($"{jill.Name} has {jill.Children.Count} children.");
            WriteLine(
                format: "{0}'s first child is called {1}",
                arg0: harry.Name,
                arg1: harry.Children[0].Name
                );

            WriteLine($"5! is {Person.Factorial(5)}");


            // assign the method defined in this program to the delegate field
            // note: delegates are multicast so can assign multiple delegates to a single delegate field although no control as to order called
            // so could have used += operator instead of just the = assignment operator (book page 189)
            harry.Shout += Harry_Shout;

            harry.Poke();
            harry.Poke();
            harry.Poke();
            harry.Poke();

            // BOOK: page 192 INTERFACES
            Person[] people =
            {
                new Person("Simon"),
                new Person("Jenny"),
                new Person("Adam"),
                new Person("Richard")
            };

            WriteLine("People unsorted: ");
            foreach (var person in people)
            {
                Write($"{person.Name} ");
            }
            WriteLine();

            WriteLine("People sorted - uses IComparable implementation to sort: ");
            Array.Sort(people);
            foreach (var person in people)
            {
                Write($"{person.Name} ");
            }
            WriteLine();


            WriteLine("People sorted - uses PersonComparer (implements IComparer) class sort: ");
            Array.Sort(people, new PersonComparer());
            foreach (var person in people)
            {
                Write($"{person.Name} ");
            }
            WriteLine();

            // BOOK: page 198 making types safely reusable with GENERICS
            var t1 = new Thing();

            t1.Data = 42;
            WriteLine($"Thing with an integer: {t1.Process(42)}");

            var t2 = new Thing();

            t2.Data = "apple";
            WriteLine($"Thing with an string: {t2.Process("apple")}");

            // on instantiating instance of generic type, must pass a type parameter
            var gt1 = new GenericThing <int>();

            gt1.Data = 42;
            WriteLine($"GenericThing with an integer: {gt1.Process(42)}");

            var gt2 = new GenericThing <string>();

            gt2.Data = "apple";
            WriteLine($"GenericThing with an string: {gt2.Process("apple")}");

            string number1 = "4";

            WriteLine("{0} squared is {1}",
                      arg0: number1,
                      arg1: Squarer.Square <string>(number1));

            byte number2 = 3;

            WriteLine("{0} squared is {1}",
                      arg0: number1,
                      arg1: Squarer.Square <byte>(number2));

            var vector1 = new DisplacementVector(3, 4);
            var vector2 = new DisplacementVector(-2, 1);
            var vector3 = vector1 + vector2;

            WriteLine($"vector3: {vector3.X}, {vector3.Y}");

            Employee john = new Employee
            {
                Name        = "John Jones",
                DateOfBirth = new DateTime(1990, 7, 28)
            };

            john.WriteToConsole();
            john.EmployeeCode = "JJ001";
            john.HireDate     = new DateTime(2014, 11, 23);
            WriteLine($"{john.Name} was hired on {john.HireDate:dd/MM/yy} with code {john.EmployeeCode}");

            WriteLine(john.ToString());
            WriteLine();

            // BOOK: page 212 Understanding polymorphism ---------------------------------------------------------------------

            // implicit casting section ****
            // an instance of a derived type (Employee) can be stored in a variable of the base type (Person) - IMPLICIT CASTING
            Employee aliceEmployee = new Employee {
                Name = "Alice", EmployeeCode = "AA123"
            };
            Person alicePerson = aliceEmployee;  // IMPLICIT casting

            WriteLine("Understanding Polymorphism section: ");
            WriteLine("alicePerson has just been assigned to aliceEmployee i.e. alicePerson = aliceEmployee");
            WriteLine();
            WriteLine("Now calling aliceEmployee.WriteToConsole(), then alicePerson.WriteToConsole()");
            aliceEmployee.WriteToConsole();                                             // Employee.WriteToConsole(): Alice was born on 01/01/01 and hired on 01/01/01
            alicePerson.WriteToConsole();                                               // Person.WriteToConsole(): Alice was born on a Monday
            WriteLine("Calling aliceEmployee.ToString(): " + aliceEmployee.ToString()); // Employee.ToString(): Alice's code is AA123
            WriteLine("Calling alicePerson.ToString(): " + alicePerson.ToString());     // Employee.ToString(): Alice's code is AA123

            // explicit casting section - and avoiding Casting Exceptions two options: use 'is' or 'as' keywords ****

            // use keyword 'is'
            if (alicePerson is Employee)
            {
                WriteLine($"{nameof(alicePerson)} IS an Employee - because of earlier assignment: alicePerson = aliceEmployee");
                // safe to cast
                Employee explicitAlice = (Employee)alicePerson;
            }

            Employee aliceAsEmployee = alicePerson as Employee; // keyword 'as' used to explicity cast --> need to check for null though

            if (aliceAsEmployee != null)
            {
                WriteLine($"{nameof(alicePerson)} AS an Employee - because of earlier assignment: Employee aliceAsEmployee = alicePerson as Employee");
            }



            // BOOK: page 216 Inheriting exceptions  ---------------------------------------------------------------------
            try
            {
                john.TimeTravel(new DateTime(1999, 12, 31));
                john.TimeTravel(new DateTime(1950, 12, 25));
            }
            catch (PersonException ex)
            {
                WriteLine(ex.Message);
            }



            // BOOK: page 218 Extending types when you cannot inherit (sealed)   ---------------------------------------------------------------------
            string email1 = "*****@*****.**";
            string email2 = "test&test.com";

            WriteLine("{0} is a valid email address: {1}",
                      arg0: email1,
                      arg1: StringExtensions.IsValidEmail(email1));

            WriteLine("{0} is a valid email address: {1}",
                      arg0: email2,
                      arg1: StringExtensions.IsValidEmail(email2));

            WriteLine("{0} is a valid email address: {1}",
                      arg0: email1,
                      arg1: email1.IsValidEmail());

            WriteLine("{0} is a valid email address: {1}",
                      arg0: email2,
                      arg1: email2.IsValidEmail());
        }
Example #2
0
        static void Main(string[] args)
        {
            var harry = new Person {
                Name = "Harry"
            };
            var mary = new Person {
                Name = "Mary"
            };
            var jill = new Person {
                Name = "Jill"
            };

            // call instance method
            var baby1 = mary.Procreate(harry);

            // call static method
            var baby2 = Person.Procreate(harry, jill);

            // call an operator
            var baby3 = harry * mary;

            WriteLine($"{harry.Name} has {harry.Children.Count} children");
            WriteLine($"{mary.Name} has {mary.Children.Count} children");
            WriteLine($"{jill.Name} has {jill.Children.Count} children");

            WriteLine(
                $"{harry.Name}'s first child is named \"{harry.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 t1 = new Thing();

            t1.Data = 42;

            WriteLine($"Thing with an integer: {t1.Process(42)}");

            var t2 = new Thing();

            t2.Data = "apple";

            WriteLine($"Thing with a string: {t2.Process("apple")}");

            var gt1 = new GenericThing <int>();

            gt1.Data = 42;

            WriteLine($"GenericThing with an integer: {gt1.Process(42)}");

            var gt2 = new GenericThing <string>();

            gt2.Data = "apple";

            WriteLine($"GenericThing with a string: {gt2.Process("apple")}");

            string number1 = "4";

            WriteLine($"{number1} squared is {Squarer.Square<string>(number1)}");

            byte number2 = 3;

            WriteLine($"{number2} squared is {Squarer.Square(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 john = new Employee
            {
                Name        = "John Jones",
                DateOfBirth = new DateTime(1990, 7, 28)
            };

            john.WriteToConsole();

            john.EmployeeCode = "JJ001";
            john.HireDate     = new DateTime(2014, 11, 23);

            WriteLine($"{john.Name} was hired on {john.HireDate:dd/MM/yy}");

            WriteLine(john.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 explicitAlice = (Employee)aliceInPerson;
            }

            try
            {
                john.TimeTravel(new DateTime(1999, 12, 31));
                john.TimeTravel(new DateTime(1950, 12, 25));
            }
            catch (PersonException ex)
            {
                WriteLine(ex.Message);
            }

            string email1 = "*****@*****.**";
            string email2 = "ian&test.com";

            WriteLine(
                "{0} is a valid email address: {1}",
                email1,
                email1.IsValidEmail()
                );

            WriteLine(
                "{0} is a valid email address: {1}",
                email2,
                email2.IsValidEmail()
                );
        }
Example #3
0
        // Sample

        static void Main(string[] args)
        {
            var harry = new Person {
                Name = "Harry"
            };
            var mary = new Person {
                Name = "Mary"
            };
            var jill = new Person {
                Name = "Jill"
            };

            // Implementing functionality using methods and operators

            // 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($"{harry.Name} has {harry.Children.Count} children.");
            WriteLine($"{mary.Name} has {mary.Children.Count} children.");
            WriteLine($"{jill.Name} has {jill.Children.Count} children.");

            WriteLine(
                format: "{0}'s first child is named \"{1}\".",
                arg0: harry.Name,
                arg1: harry.Children[0].Name);

            // Implementing functionality using local functions

            WriteLine($"5! is {Person.Factorial(5)}");

            // Raising and handling events

            harry.Shout += Harry_Shout;

            harry.Poke();
            harry.Poke();
            harry.Poke();
            harry.Poke();

            // Comparing objects when sorting

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

            // Comparing objects using a separate class

            WriteLine("Use PersonComparer's IComparer implementation to sort:");
            Array.Sort(people, new PersonComparer());
            foreach (var person in people)
            {
                WriteLine($"{person.Name}");
            }

            // Working with generic types

            var t1 = new Thing();

            t1.Data = 42;
            WriteLine($"Thing with an integer: {t1.Process(42)}");

            var t2 = new Thing();

            t2.Data = "apple";
            WriteLine($"Thing with a string: {t2.Process("apple")}");

            var gt1 = new GenericThing <int>();

            gt1.Data = 42;
            WriteLine($"GenericThing with an integer: {gt1.Process(42)}");

            var gt2 = new GenericThing <string>();

            gt2.Data = "apple";
            WriteLine($"GenericThing with a string: {gt2.Process("apple")}");

            // Working with generic methods

            string number1 = "4";

            WriteLine("{0} squared is {1}",
                      arg0: number1,
                      arg1: Squarer.Square <string>(number1));

            byte number2 = 3;

            WriteLine("{0} squared is {1}",
                      arg0: number2,
                      arg1: Squarer.Square(number2));

            // Working with struct types

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

            // Inheriting from classes

            Employee john = new Employee
            {
                Name        = "John Jones",
                DateOfBirth = new DateTime(1990, 7, 28)
            };

            john.WriteToConsole();

            // Extending classes

            john.EmployeeCode = "JJ001";
            john.HireDate     = new DateTime(2014, 11, 23);
            WriteLine($"{john.Name} was hired on {john.HireDate:dd/MM/yy}");

            // Overriding  members

            WriteLine(john.ToString());

            // Understanding polymorphism

            Employee aliceInEmployee = new Employee
            {
                Name = "Alice", EmployeeCode = "AA123"
            };

            Person aliceInPerson = aliceInEmployee;

            aliceInEmployee.WriteToConsole();

            aliceInPerson.WriteToConsole();

            WriteLine(aliceInEmployee.ToString());

            WriteLine(aliceInPerson.ToString());

            // Explicit casting

            if (aliceInPerson is Employee)
            {
                WriteLine($"{nameof(aliceInPerson)} IS an Employee");

                Employee explicitAlice = (Employee)aliceInPerson;
                // safely do something with explicitAlice
            }

            Employee aliceAsEmployee = aliceInPerson as Employee;

            if (aliceAsEmployee != null)
            {
                WriteLine($"{nameof(aliceInPerson)} AS an Employee");
                // do something with aliceInEmployee
            }

            // Inheriting exceptions

            try
            {
                john.TimeTravel(new DateTime(1999, 12, 31));
                john.TimeTravel(new DateTime(1950, 12, 25));
            }
            catch (PersonException ex)
            {
                WriteLine(ex.Message);
            }

            // Using static methods to reuse functionality

            string email1 = "*****@*****.**";
            string email2 = "ian&test.com";

            WriteLine(
                "{0} is a valid e-mail address: {1}",
                arg0: email1,
                arg1: StringExtensions.IsValidEmail(email1));

            WriteLine(
                "{0} is a valid e-mail address: {1}",
                arg0: email2,
                arg1: StringExtensions.IsValidEmail(email2));

            WriteLine(
                "{0} is a valid e-mail address: {1}",
                arg0: email1,
                arg1: email1.IsValidEmail());

            WriteLine(
                "{0} is a valid e-mail address: {1}",
                arg0: email2,
                arg1: email2.IsValidEmail());
        }
Example #4
0
        static void Main(string[] args)
        {
            /* Ch05 Commented for clarity while working through Chapter 06
             * var bob = new Person();
             * bob.Name = "Bob Smith";
             * bob.DateOfBirth = new DateTime(1965, 12, 22);
             * bob.FavoriteAncientWonder = WondersOfTheAncientWorld.StatusOfZeusAtOlympia;
             * var alice = new Person
             * {
             *  Name = "Alice Jones",
             *  DateOfBirth = new DateTime(1998, 3, 7)
             * };
             *
             * WriteLine(format: "{0} was born on {1: dddd, d MMMM yyyy}",
             * arg0: bob.Name,
             * arg1: bob.DateOfBirth);
             *
             * WriteLine(format: "{0} was born on {1:dd MMM yy}", arg0: alice.Name,
             * arg1: alice.DateOfBirth);
             * 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;
             * // Another option is to set the value by casting 18 to the enum type, but this is more difficult 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}");
             * }
             * // as a bonus, use a foreach statement
             * foreach (var children in bob.Children)
             * {
             *  WriteLine($"{children.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);
             *
             * // methods
             * bob.WriteToConsole();
             * WriteLine(bob.GetOrigin());
             *
             * // using tuples
             * (string, int) fruit = bob.GetFruit();
             * WriteLine($"{fruit.Item1}, {fruit.Item2} there are");
             *
             * // named tuples
             * var fruitNamed = bob.GetNamedFruit();
             * WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}.");
             * var thing1 = ("Neville", 4);
             *
             * // inferring tuple names
             * WriteLine($"{thing1.Item1} has {thing1.Item2} children.");
             * var thing2 = (bob.Name, bob.Children.Count);
             * WriteLine($"{thing2.Name} has {thing2.Count} children.");
             *
             * // deconstructed
             * (string fruitName, int fruitNumber) = bob.GetFruit();
             * WriteLine($"Deconstructed: {fruitName}, {fruitNumber}");
             *
             * // passing parameters to methods + overloading methods
             * WriteLine(bob.SayHello());
             * WriteLine(bob.SayHello("Emily"));
             *
             * // optional parameters
             * WriteLine(bob.OptionalParameters());
             * // inferred by position
             * WriteLine(bob.OptionalParameters("Jump!", 98.5));
             * // select using named params
             * WriteLine(bob.OptionalParameters(number: 52.7, command: "Hide!"));
             * // use named parameters to skip over optional params
             * 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.0 syntax for 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}.");
             *
             * // 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}");
             ***END BLOCK COMMENT *** END BLOCK COMMENT */

            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($"{harry.Name} has {harry.Children.Count} children.");
            WriteLine($"{mary.Name} has {mary.Children.Count} children.");
            WriteLine($"{jill.Name} has {jill.Children.Count} children");
            WriteLine(format: "{0}'s first child is named \"{1}\".", arg0: harry.Name, arg1: harry.Children[0].Name);
            WriteLine($"5! is {Person.Factorial(5)}");

            harry.Shout += Harry_Shout; // "By assigning a method, () would replace
            harry.Poke();
            harry.Poke();
            harry.Poke();
            harry.Poke();

            Person[] people =
            {
                new Person {
                    Name = "Simon"
                },
                new Person {
                    Name = "Jimmy"
                },
                new Person {
                    Name = "Richard"
                },
                new Person {
                    Name = "Adam"
                },
            };
            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 t1 = new Thing();

            t1.Data = 42;
            WriteLine($"Thing with an integer: {t1.Process(42)}");

            var t2 = new Thing();

            t2.Data = "apple";
            WriteLine($"Thing with a string: {t2.Process("apple")}");

            var gt1 = new GenericThing <int>();

            gt1.Data = 42;
            WriteLine($"GenericThing with an integer: {gt1.Process(42)}");

            var gt2 = new GenericThing <string>();

            gt2.Data = "Apple";
            WriteLine($"GenericThing with an string: {gt2.Process("Apple")}");

            string number1 = "4";

            WriteLine($"{number1} squared is {Squarer.Square<string>(number1)}");

            byte number2 = 3;

            WriteLine($"{number2} squared is {Squarer.Square(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 john = new Employee
            {
                Name        = "John Jones",
                DateOfBirth = new DateTime(1990, 7, 28)
            };

            john.WriteToConsole();

            john.EmployeeCode = "JJ001";
            john.HireDate     = new DateTime(2014, 11, 23);
            WriteLine($"{john.Name} was hired on {john.HireDate:dd/MM/yy}");
            // Writes Packt.Shared.Employee
            WriteLine(john.ToString());


            Employee aliceInEmployee = new Employee
            {
                Name         = "Alice",
                EmployeeCode = "AA123"
            };

            Person aliceInPerson = aliceInEmployee;

            aliceInEmployee.WriteToConsole();
            aliceInPerson.WriteToConsole();
            WriteLine(aliceInEmployee.ToString());
            WriteLine(aliceInPerson.ToString());
            // explicit casting
            // Employee explicitAlice = (Employee)aliceInPerson;

            if (aliceInPerson is Employee)
            {
                WriteLine($"{nameof(aliceInPerson)} IS an Employee");
                Employee explicitAlice = (Employee)aliceInPerson;
                // safely do something with explicitAlice
            }

            Employee aliceAsEmployee = aliceInPerson as Employee;

            if (aliceAsEmployee != null)
            {
                WriteLine($"{nameof(aliceInPerson)} AS an Employee");
                // do something with aliceAsEmployee
            }
            // Inheriting Exceptions
            try
            {
                john.TimeTravel(new DateTime(1999, 12, 31));
                john.TimeTravel(new DateTime(1950, 12, 25));
            }
            catch (PersonException ex)
            {
                WriteLine(ex.Message);
            }
            // Using Static methods to reuse functionality
            string email1 = "*****@*****.**";
            string email2 = "ian&test.com";

            /*
             * WriteLine(
             * "{0} is a valid e-mail address: {1}",
             * arg0: email1,
             * arg1: StringExtensions.IsValidEmail(email1));
             * WriteLine(
             * "{0} is a valid e-mail address: {1}",
             * arg0: email2,
             * arg1: StringExtensions.IsValidEmail(email2));
             */

            // Using Extension Methods to reuse functionality
            WriteLine(
                "{0} is a valid e-mail address: {1}",
                arg0: email1,
                arg1: email1.IsValidEmail());
            WriteLine(
                "{0} is a valid e-mail address: {1}",
                arg0: email2,
                arg1: email2.IsValidEmail());
        }
Example #5
0
        static void Main(string[] args)
        {
            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 = "Juliani"
                },
                new Person {
                    Name = "Simon"
                },
                new Person {
                    Name = "Zoraide"
                },
                new Person {
                    Name = "Alan"
                },
                new Person {
                    Name = "Pedro"
                },
                new Person {
                    Name = "Beatriz"
                }
            };

            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: {StringExtensions.IsValidEmail(email1)}.");
            WriteLine($"{email2} is a valid e-mail address: {StringExtensions.IsValidEmail(email2)}.");

            WriteLine($"{email1} is a valid e-mail address:{email1.IsValidEmail()}.");
            WriteLine($"{email2} is a valid e-mail address:{email2.IsValidEmail()}.");
        }