Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Clear();

            WriteLine("-- People App --");

            var bob = new Person();

            bob.Name        = "Bob Smith";
            bob.DateOfBirth = new DateTime(1956, 8, 24);

            WriteLine($"{bob.Name} was born on " +
                      $"{bob.DateOfBirth:dddd, d MMMM yyyy}");

            WriteLine($"{bob.Name} was born on {bob.HomePlanet}");

            WriteLine($"{bob.Name} is a {Person.Species}");

            bob.Children.Add(new Person {
                Name = "Alfred"
            });
            bob.Children.Add(new Person {
                Name = "Jessica"
            });

            for (int child = 0; child < bob.Children.Count; child++)
            {
                WriteLine($"{bob.Children[child].Name} is Bob's Child.");
            }


            BankAccount.InterestRate = 0.012M;          // Store a shared value

            var alfredAccount = new BankAccount();      // Instance BankAccount

            alfredAccount.AccountName = "Mrs. Alfred";
            alfredAccount.Balance     = 2400;

            WriteLine(format: "{0} earned {1:C} interest.",
                      arg0: alfredAccount.AccountName,
                      arg1: alfredAccount.Balance * BankAccount.InterestRate);

            var jessicaAccount = new BankAccount();     // Instance BankAccount

            jessicaAccount.AccountName = "Ms. Jessica";
            jessicaAccount.Balance     = 5600;

            WriteLine(format: "{0} earned {1:C} interest.",
                      arg0: jessicaAccount.AccountName,
                      arg1: jessicaAccount.Balance * BankAccount.InterestRate);


            // Blank Person for Constructor
            var blankPerson = new Person();

            WriteLine(format: "{0} of {1} was created at {2:hh:mm:ss} " +
                      "on a {2:dddd}.",
                      arg0: blankPerson.Name,
                      arg1: blankPerson.HomePlanet,
                      arg2: blankPerson.Instantiated);

            var gunny = new Person("Gunny", "Mars");

            WriteLine(format: "{0} of {1} was created at {2:hh:mm:ss} " +
                      "on a {2:dddd}.",
                      arg0: gunny.Name,
                      arg1: gunny.HomePlanet,
                      arg2: gunny.Instantiated);

            // Defauls
            var thingOfDefault = new ThingOfDefaults();

            WriteLine("Default Values");
            WriteLine($"{thingOfDefault.Population} - {thingOfDefault.When} - " +
                      $"{thingOfDefault.Name} - {thingOfDefault.People}");


            // METHOD CALL
            bob.WriteToConsole();
            WriteLine(bob.GetOrigin());

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

            // Return Value
            var toReturn = new Processor();
            var result   = toReturn.GetTheData();

            WriteLine(result.Text);
            WriteLine(result.Number);

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

            (string, int)fruit = bob.GetFruit();
            WriteLine($"{fruit.Item1}, {fruit.Item2} there are.");

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

            var thing1 = ("Neville", 4);
            var thing2 = (bob.Name, bob.Children.Count);

            WriteLine($"ThingOne = {thing1.Item1} has {thing1.Item2} children.");
            WriteLine($"ThingTwo = {thing2.Name} has {thing2.Count} children.");

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

            (string name, int age)deconstruct = bob.GetPerson();
            WriteLine($"{deconstruct.name} is {deconstruct.age} yeas old!");

            WriteLine(new string('-', 35));
            WriteLine(bob.SayHello("Deve"));
            WriteLine(bob.SayHelloTo("Firat"));
            WriteLine(gunny.DirtyTalk("Zombi"));
            WriteLine(gunny.SaySomething("Blah Blash!"));

            WriteLine();

            WriteLine(new string('-', 35));
            int a = 10;
            int b = 20;
            int c = 30;

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

            WriteLine();

            WriteLine(new string('-', 35));
            var sam = new Person
            {
                Name        = "Sam",
                DateOfBirth = new DateTime(1972, 1, 27)
            };

            sam.FavoriteIceCream     = "Chocolate Fudge";
            sam.FavoritePrimaryColor = "Red";

            WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}");
            WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}");

            WriteLine(sam.Origin);
            WriteLine(sam.Greeting);
            WriteLine($"Sam {sam.Age} yeas old!");

            WriteLine();

            WriteLine(new string('-', 35));
            sam.Children.Add(new Person {
                Name = "Charlie"
            });
            sam.Children.Add(new Person {
                Name = "Ella"
            });

            WriteLine($"Sam's first child is {sam.Children[0].Name}");
            WriteLine($"Sam's second child is {sam.Children[1].Name}");

            WriteLine($"Sam's first child is {sam[0].Name}");
            WriteLine($"Sam's second child is {sam[1].Name}");

            WriteLine();

            WriteLine(new string('-', 35));
            // -----------------------------------------------------------------
            object[] passengers =
            {
                new FirstClassPassenger {
                    AirMiles = 1_419
                },
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var bob = new Person();

            bob.Name        = "Bob Smith";
            bob.DateOfBirth = new DateTime(1965, 12, 22);
            WriteLine(
                format: "{0} was born on {1:dddd, d MMMM yyyy}",
                arg0: bob.Name,
                arg1: bob.DateOfBirth);
            var alice = new Person
            {
                Name        = "Alice Jones",
                DateOfBirth = new DateTime(1998, 3, 7)
            };

            WriteLine(
                format: "{0} was born on {1:dd MMM yy}",
                arg0: alice.Name,
                arg1: alice.DateOfBirth);

            bob.FavoriteAncientWonder =
                WondersOfTheAncientWorld.StatueOfZeusAtOlympia;
            WriteLine(format:
                      "{0}'s favorite wonder is {1}. It's integer is {2}.",
                      arg0: bob.Name,
                      arg1: bob.FavoriteAncientWonder,
                      arg2: (int)bob.FavoriteAncientWonder);

            bob.BucketList =
                WondersOfTheAncientWorld.HangingGardensOfBabylon
                | WondersOfTheAncientWorld.MausoleumAtHalicarnassus;
            // bob.BucketList = (WondersOfTheAncientWorld)18;
            WriteLine($"{bob.Name}'s bucket list is {bob.BucketList}");

            bob.Children.Add(new Person {
                Name = "Alfred"
            });
            bob.Children.Add(new Person {
                Name = "Zoe"
            });

            WriteLine(
                $"{bob.Name} has {bob.Children.Count} children:");
            for (int child = 0; child < bob.Children.Count; child++)
            {
                WriteLine($"  {bob.Children[child].Name}");
            }

            bob.Children.Add(new Person {
                Name = "Alfred"
            });
            bob.Children.Add(new Person {
                Name = "Zoe"
            });

            WriteLine(
                $"{bob.Name} has {bob.Children.Count} children:");
            for (int child = 0; child < bob.Children.Count; child++)
            {
                WriteLine($"  {bob.Children[child].Name}");
            }

            BankAccount.InterestRate = 0.012M; // store a shared value
            var jonesAccount = new BankAccount();

            jonesAccount.AccountName = "Mrs. Jones";
            jonesAccount.Balance     = 2400;
            WriteLine(format: "{0} earned {1:C} interest.",
                      arg0: jonesAccount.AccountName,
                      arg1: jonesAccount.Balance * BankAccount.InterestRate);
            var gerrierAccount = new BankAccount();

            gerrierAccount.AccountName = "Ms. Gerrier";
            gerrierAccount.Balance     = 98;
            WriteLine(format: "{0} earned {1:C} interest.",
                      arg0: gerrierAccount.AccountName,
                      arg1: gerrierAccount.Balance * BankAccount.InterestRate);

            WriteLine($"{bob.Name} is a {Person.Species}");

            WriteLine($"{bob.Name} was born on {bob.HomePlanet}");

            var blankPerson = new Person();

            WriteLine(format:
                      "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.",
                      arg0: blankPerson.Name,
                      arg1: blankPerson.HomePlanet,
                      arg2: blankPerson.Instantiated);

            var gunny = new Person("Gunny", "Mars");

            WriteLine(format:
                      "{0} of {1} was created at {2:hh:mm:ss} on a {2:dddd}.",
                      arg0: gunny.Name,
                      arg1: gunny.HomePlanet,
                      arg2: gunny.Instantiated);

            bob.WriteToConsole();
            WriteLine(bob.GetOrigin());

            (string, int)fruit = bob.GetFruit();
            WriteLine($"{fruit.Item1}, {fruit.Item2} there are.");

            var fruitNamed = bob.GetNamedFruit();

            WriteLine($"There are {fruitNamed.Number} {fruitNamed.Name}.");

            var thing1 = ("Neville", 4);

            WriteLine($"{thing1.Item1} has {thing1.Item2} children.");
            var thing2 = (bob.Name, bob.Children.Count);

            WriteLine($"{thing2.Name} has {thing2.Count} children.");

            (string fruitName, int fruitNumber) = bob.GetFruit();
            WriteLine($"Deconstructed: {fruitName}, {fruitNumber}");

            WriteLine(bob.SayHello());
            WriteLine(bob.SayHelloTo("Emily"));

            WriteLine(bob.OptionalParameters());

            WriteLine(bob.OptionalParameters("Jump!", 98.5));

            WriteLine(bob.OptionalParameters(
                          number: 52.7, command: "Hide!"));

            WriteLine(bob.OptionalParameters("Poke!", active: false));

            int a = 10;
            int b = 20;
            int c = 30;

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

            int d = 10;
            int e = 20;

            WriteLine(
                $"Before: d = {d}, e = {e}, f doesn't exist yet!");
            // simplified C# 7.0 syntax for the out parameter
            bob.PassingParameters(d, ref e, out int f);
            WriteLine($"After: d = {d}, e = {e}, f = {f}");

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

            WriteLine(sam.Origin);
            WriteLine(sam.Greeting);
            WriteLine(sam.Age);

            sam.FavoriteIceCream = "Chocolate Fudge";
            WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}.");
            sam.FavoritePrimaryColor = "Red";
            WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}.");

            sam.Children.Add(new Person {
                Name = "Charlie"
            });
            sam.Children.Add(new Person {
                Name = "Ella"
            });
            WriteLine($"Sam's first child is {sam.Children[0].Name}");
            WriteLine($"Sam's second child is {sam.Children[1].Name}");
            WriteLine($"Sam's first child is {sam[0].Name}");
            WriteLine($"Sam's second child is {sam[1].Name}");
        }