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

            var baby1 = mary.ProcreateWith(harry);
            var baby2 = Person.Procreate(harry, jill);

            // call 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(new string('-', 50));
            WriteLine($"5! is {Person.Factorial(5)}");

            // delegates
            WriteLine(new string('-', 50));
            var p1     = new Person();
            int answer = p1.MethodIWantToCall("Frog");

            WriteLine(answer);

            var d       = new DelegateWithMatchingSignature(p1.MethodIWantToCall);
            int answer2 = d("Frog");

            WriteLine(answer2);

            WriteLine(new string('-', 50));
            harry.Shout += Harry_Shout;
            harry.Poke();
            harry.Poke();
            harry.Poke();
            harry.Poke();

            WriteLine(new string('-', 50));
            Person[] people =
            {
                new Person {
                    Name = "Simon"
                },
                new Person {
                    Name = "Jenny"
                },
                new Person {
                    Name = "Adam"
                },
                new Person {
                    Name = "Richard"
                }
            };

            WriteLine("Initial list for 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}");
            }

            WriteLine(new string('-', 50));

            var t = new Thing();

            t.Data = 42;
            WriteLine($"Thing : {t.Process("42")}");

            // generic
            var gt = new GenericThing <int>();

            gt.Data = 42;
            WriteLine($"Generic thing: {gt.Process("42")}");

            WriteLine(new string('-', 50));
            Employee e1 = new Employee
            {
                Name     = "John Jones",
                HireDate = new DateTime(1990, 7, 28)
            };

            e1.WriteToConsole();

            WriteLine(new string('-', 50));
            WriteLine(e1.ToString());


            WriteLine(new string('-', 50));
            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;
            }

            Employee e3 = aliceInPerson as Employee;

            if (e3 != null)
            {
                WriteLine($"{nameof(aliceInPerson)} IS an Employee");
            }

            WriteLine(new string('-', 50));
            try
            {
                e1.TimeTravel(new DateTime(1999, 12, 31));
                e1.TimeTravel(new DateTime(1950, 12, 25));
            }
            catch (PersonException ex)
            {
                WriteLine(ex.Message);
            }
            WriteLine(new string('-', 50));
            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()}");
        }
예제 #2
0
        static void Chapter06()
        {
            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);

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

            // factorial with local function
            WriteLine($"5! is {Person.Factorial(5)}");

            // Delegate example code
            var p1 = new Person("Delegate");

            // normal way
            int answer = p1.MethodIWantToCall(p1.Name);

            // assign defined delegate function to variable
            var d = new DelegateWithMatchingSignature(p1.MethodIWantToCall);
            int delegatedAnswer = d("Frog");

            WriteLine($"Delegated Answer: {delegatedAnswer}");

            // delegate example
            harry.Shout += Harry_Shout;
            harry.Poke();
            harry.Poke();
            harry.Poke();
            harry.Poke();

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

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

            // not using generics - generates warning - comparison not valid
            var t = new Thing();

            t.Data = 42;
            WriteLine($"Thing: {t.Process("42")}");

            // using generics - define type in <brackets>
            var gt = new GenericThing <int>();

            gt.Data = 42;
            WriteLine($"GenericThing: {gt.Process("42")}");

            // using generic methods in non-generic class
            string number1 = "4";

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

            byte number2 = 3;

            WriteLine($"{number2} squared is {Squarer.Square<byte>(number2)}");

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

            // Using Employee (inheriting from Person)
            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}");

            // uses System.Object.ToString() until overriden.
            WriteLine(e1.ToString());

            // demonstrate difference between polymorphism and non-polymorphism
            // "<blank> new" = non-polymorhpic
            // "virtual override" = polymorphic
            Employee aliceInEmployee = new Employee {
                Name = "Alice", EmployeeCode = "AA123"
            };
            Person aliceInPerson = aliceInEmployee;

            aliceInEmployee.WriteToConsole();
            // doesn't use the WriteToConsole override
            aliceInPerson.WriteToConsole();
            WriteLine(aliceInEmployee.ToString());
            // still uses overriden method in Employee Class
            WriteLine(aliceInPerson.ToString());

            // checking class is type we expect it to be.
            if (aliceInPerson is Employee)
            {
                WriteLine($"{nameof(aliceInPerson)} IS an employee");
                Employee e2 = (Employee)aliceInPerson;
                // do something with e2
            }

            // or, convert is possible, if unavailable as returns null
            Employee e3 = aliceInPerson as Employee;

            if (e3 != null)
            {
                WriteLine($"{nameof(aliceInPerson)} AS an Employee");
            }

            // use custom exception
            try
            {
                e1.TimeTravel(new DateTime(1999, 12, 31));
                e1.TimeTravel(new DateTime(1950, 12, 25));
            }
            catch (PersonException ex)
            {
                WriteLine(ex.Message);
            }

            // adding methods to string
            string email1 = "*****@*****.**";
            string email2 = "pamela&test.com";

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

            var p1 = new Person {
                Name = "Joe"
            };
            int answer = p1.MethodIWantToCall("Frog");

            WriteLine($"instance->{answer}");

            var d = new DelegateWithMatchingSignature(p1.MethodIWantToCall);

            int answer2 = d("Frog");

            WriteLine($"delegate->{answer2}");

            harry.Shout += Harry_Shout;
            harry.Poked();
            harry.Poked();
            harry.Poked();
            harry.Poked();

            Person[] people =
            {
                new Person {
                    Name = "Simon"
                },
                new Person {
                    Name = "Cathy"
                },
                new Person {
                    Name = "Judy"
                },
                new Person {
                    Name = "Adam"
                }
            };

            WriteLine("initial list of people:");
            foreach (var person in people)
            {
                WriteLine($"{person.Name}");
            }

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

            WriteLine("Use PersonComparer's IComparer to 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>();

            WriteLine($"{gt1.ToString()}");
            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("{0} squuared is {1}", arg0: number1, arg1: Squarer.Square <string>(number1));

            byte number2 = 3;

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

            var dv1 = new DisplacementVector(2, 5);
            var dv2 = new DisplacementVector(-1, 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:yyyy/MMM/dd}");
            john.WriteToConsole();
            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;
            }

            Employee aliceAsEmployee = aliceInPerson as Employee;

            if (aliceAsEmployee != null)
            {
                WriteLine($"{nameof(aliceAsEmployee)} as an Employee");
            }

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

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

            WriteLine("{0} is a valid e-mail address: {1}",
                      arg0: email1, arg1: StringExtension.IsValidEmail(email1));
            WriteLine("{0} is a valid e-mail address: {1}",
                      arg0: email2, arg1: StringExtension.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());
        }
예제 #4
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);

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

            string s1 = "Hello ";
            string s2 = "World!";
            // string s3 = string.Concat(s1, s2);
            string s3 = s1 + s2;

            WriteLine(s3);

            WriteLine($"5! is {Person.Factorial(5)}");
            var p1      = new Person();
            var answer1 = p1.MethodIWantToCall("Frog");
            var d       = new DelegateWithMatchingSignature(new Person().MethodIWantToCall);
            var answer2 = d("Frog");

            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 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("{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));

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

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

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