Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            // Person1
            Person1 p1 = new Person1("Jack", 28);

            p1.name = "Jill";
            Console.WriteLine(p1.age);

            // Person2
            Person2 p2 = new Person2("Jack", 28);

            p2.SetName("Jill");
            Console.WriteLine(p2.GetAge());

            // Person3
            Person3 p3 = new Person3("Jack", 28);

            p3.Name = "Jill";
            Console.WriteLine(p3.Age);

            Person5 p5 = new Person5("Jack", 30);

            p5.Age = 100;
            Console.WriteLine(p5.Age);

            Console.ReadLine();
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            // Person1
            Person1 p1 = new Person1("Jack", 28);
            p1.name = "Jill";
            Console.WriteLine(p1.age);

            // Person2
            Person2 p2 = new Person2("Jack", 28);
            p2.SetName("Jill");
            Console.WriteLine(p2.GetAge());

            // Person3
            Person3 p3 = new Person3("Jack", 28);
            p3.Name = "Jill";
            Console.WriteLine(p3.Age);

            Person4 p4 = new Person4("Jack", 28);
            //p4.Name = "Jill";
            Console.WriteLine(p4.Age);

            // Person5
            Person5 p5 = new Person5("Jack", 30);
            p5.Age = -5;
            Console.WriteLine(p5.Age);
        }
Exemplo n.º 3
0
        public static void Main(string[] args)
        {
            // Create an object
            Person1 p1 = new Person1("Jack", 21);

            // Create a struct
            Person2 p2 = new Person2("Jack", 21);

            // There's always a zero-argument constructor for structs
            Person2 x = new Person2();

            // Make both persons one year older
            Older1(p1);
            Older2(p2);

            // Note the difference in the two ages
            Console.WriteLine("Both persons one year older");
            Console.WriteLine("p1.Age = " + p1.Age);
            Console.WriteLine("p2.Age = " + p2.Age);
            Console.ReadLine();

            // Pass 2 by reference and it works out
            Older3(ref p2);
            Console.WriteLine("Successfully aging p2");
            Console.WriteLine("p2.Age = " + p2.Age);
            Console.ReadLine();

            // Create two new persons
            Person1 p3 = new Person1(p1.Name, p1.Age);
            Person2 p4 = new Person2(p2.Name, p2.Age);

            // Structs don't have == operators
            Console.WriteLine("Using the == operator");
            Console.WriteLine("p1 == p3 ? " + (p1 == p3));
            //Console.WriteLine("p2 == p4 ? " + (p2 == p4));
            Console.ReadLine();

            // Equals defaults differently for classes and structs
            Console.WriteLine("Default equality of physically different objects/structs");
            Console.WriteLine("p1.Equals(p3)) ? " + p1.Equals(p3));
            Console.WriteLine("p2.Equals(p4)) ? " + p2.Equals(p4));
            Console.ReadLine();

            // ReferenceEquals is always false for structs
            Console.WriteLine("ReferenceEquals is different for objects/structs");
            Console.WriteLine("ReferenceEquals(p1,p3)) ? " + ReferenceEquals(p1, p3));
            Console.WriteLine("ReferenceEquals(p1,p1)) ? " + ReferenceEquals(p1, p1));
            Console.WriteLine("ReferenceEquals(p2,p4)) ? " + ReferenceEquals(p2, p4));
            Console.WriteLine("ReferenceEquals(p2,p2)) ? " + ReferenceEquals(p2, p2));
        }
Exemplo n.º 4
0
        public static void Main(string[] args)
        {
            // Create an object
            Person1 p1 = new Person1("Jack", 21);

            // Create a struct
            Person2 p2 = new Person2("Jack", 21);

            // There's always a zero-argument constructor for structs
            Person2 x = new Person2();

            // Make both persons one year older
            Older1(p1);
            Older2(p2);

            // Note the difference in the two ages
            Console.WriteLine("Both persons one year older");
            Console.WriteLine("p1.Age = " + p1.Age);
            Console.WriteLine("p2.Age = " + p2.Age);
            Console.ReadLine();

            // Pass 2 by reference and it works out
            Older3(ref p2);
            Console.WriteLine("Successfully aging p2");
            Console.WriteLine("p2.Age = " + p2.Age);
            Console.ReadLine();

            // Create two new persons
            Person1 p3 = new Person1(p1.Name, p1.Age);
            Person2 p4 = new Person2(p2.Name, p2.Age);

            // Structs don't have == operators
            Console.WriteLine("Using the == operator");
            Console.WriteLine("p1 == p3 ? " + (p1 == p3));
            //Console.WriteLine("p2 == p4 ? " + (p2 == p4));
            Console.ReadLine();

            // Equals defaults differently for classes and structs
            Console.WriteLine("Default equality of physically different objects/structs");
            Console.WriteLine("p1.Equals(p3)) ? " + p1.Equals(p3));
            Console.WriteLine("p2.Equals(p4)) ? " + p2.Equals(p4));
            Console.ReadLine();

            // ReferenceEquals is always false for structs
            Console.WriteLine("ReferenceEquals is different for objects/structs");
            Console.WriteLine("ReferenceEquals(p1,p3)) ? " + ReferenceEquals(p1, p3));
            Console.WriteLine("ReferenceEquals(p1,p1)) ? " + ReferenceEquals(p1, p1));
            Console.WriteLine("ReferenceEquals(p2,p4)) ? " + ReferenceEquals(p2, p4));
            Console.WriteLine("ReferenceEquals(p2,p2)) ? " + ReferenceEquals(p2, p2));
        }
Exemplo n.º 5
0
 /// <summary>
 /// Increments the age
 /// </summary>
 public static void Older3(ref Person2 p)
 {
     p.Age++;
 }
Exemplo n.º 6
0
 /// <summary>
 /// Increments the page
 /// </summary>
 public static void Older2(Person2 p)
 {
     p.Age++;
 }
Exemplo n.º 7
0
 /// <summary>
 /// Increments the age
 /// </summary>
 public static void Older3(ref Person2 p)
 {
     p.Age++;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Increments the page
 /// </summary>
 public static void Older2(Person2 p)
 {
     p.Age++;
 }