Exemplo n.º 1
0
 public static void Main()
 {
     Person person = new Person();
     // To use the get/set methods, do the following:
     person.Name = "Evan";               // set accessor
     Console.WriteLine(person.Name);     // get accessor
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var person = new Person(new DateTime(1997, 4, 1));

            Console.WriteLine(person.Age);
        }
Exemplo n.º 3
0
        static void Main(string[] argv)
        {
            Foo x = new Foo();

            // "set" will be written to the console
            x.Bar = 5;

            // "get" will be written to the console
            int y = x.Bar;

            // Foo.Bar reflects the changes Foo.Baz makes
            x.Baz = 5;
            Console.WriteLine(x.Bar);

            // Foo.Baz ignores changes made by Foo.Bar
            x.Bar = 5;
            Console.WriteLine(x.Baz);

            Fizz z = new Fizz();

            // Nothing is written to the console
            z.Bizz = 5;
            y = z.Bizz;

            // illegal statement, Fizz.Dizz is readonly
            //z.Dizz = 5;

            // z.Dizz is null, with no way to initialize it
            y = z.Dizz;

            Jim j = new Jim();

            // Likewise, nothing is written to the console
            // z.Bizz and j.jam behave identically
            j.jam = 5;
            y = j.jam;

            // same problem as Fizz.Dizz
            //j.flam = 5;
            y = j.flam;

            // demonstrating different methods for handling a Person's age
            Person bob = new Person();
            bob.SetAge(years: 23);
            Console.WriteLine("Bob's age in years: " + bob.Age);
            Console.WriteLine("Bob's age in months: " + bob.AgeMonths);
            bob.AgeYears = 24;
            Console.WriteLine("Bob's new age: " + bob.Age);
            bob.AgeMonths = 300;
            Console.WriteLine("Bob's new age: " + bob.Age);
            

            Person baby = new Person();
            baby.SetAge(months: 2);
            Console.WriteLine("Baby's age: " + baby.Age);
            baby.AgeMonths = 3;
            Console.WriteLine("Baby's new age:" + baby.Age);
            Console.WriteLine("Baby's age in years: " + baby.AgeYears);

            // pause
            Console.ReadLine();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            var person = new Person(new DateTime(1994, 12, 20));

            Console.WriteLine(person.Age);
        }
Exemplo n.º 5
0
 static void Main(string[] args)
 {
     var person1 = new Person(new DateTime(1991, 12, 4), "FooBar");
     Console.WriteLine(person1.Age + " " + person1.Name);
 }