예제 #1
0
        /*
         * :: Differences ::
         * 1. A struct is a value type where as class is a referance type.
         * 2. All the differences apply to value type and reference type are applicable here also.
         * 3. Structs are stored in stack and classes in heap.
         * 4. Value types holds there value in the memory where they created whereas reference type hold a reference to an object in memory.
         * 5. Value types are distroyed immediately after the scope is lost, whereas for reference types only
         * the reference variable is distroyed after the scope is lost. The object is later distroyed by garbage collector.
         * 6. When you copy a struct into another struct, a new copy of struct created and modification at one struct will not affect value contained by other.
         * 7. structs can't have destructors where as class can have.
         * 8. structs can't have explicit parameter less constructor where as a class can.
         * 9. structs can't inherit from another class where as a class can. Both structs and classes can inherit from an interface.
         * 10. A class or struct cannot inherit from another struct. Structs are sealed types.
         * 11. Since struct does not support inheritance, access modifier of a member of a struct cannot be protected or protected internal.
         *
         */
        static void Main(string[] args)
        {
            #region Value types holds there value in the memory where they created whereas reference type hold a reference to an object in memory.
            // Value types are distroyed immediately after the scope is lost, whereas for reference types only
            // the reference variable is distroyed after the scope is lost. The object is later distroyed by garbage collector.
            int i = 1;

            if (i == 1)
            {
                int      j = 10;
                Customer c = new Customer();
                c.ID   = 101;
                c.Name = "Anand";
            }
            #endregion

            #region When you copy a struct into another struct, a new copy of struct created and modification at one struct will NOT affect value contained by other.

            int a = 10;
            int b = a;
            b = b + 1;
            a = a + 4; // This operation on a will not be reflected at b.
            Console.WriteLine("a = {0}, b = {1}", a, b);

            #endregion

            #region When you copy a class into another class, a only get a copy of reference variable, Both the variables point to the same onject on the heap and modification at one reference variable will affect value contained by other.

            Customer c1 = new Customer();
            c1.ID   = 100;
            c1.Name = "Anand";
            Console.WriteLine("c1.ID = {0}, c1.Name = {1}", c1.ID, c1.Name);

            Customer c2 = c1;
            c2.Name = "Sahil"; // This operation on c2.Name will be reflected at c1.Name.
            Console.WriteLine("c1.ID = {0}, c1.Name = {1}", c1.ID, c1.Name);

            #endregion

            //Struct_1 s = new Struct_1();
            //s.Equals(s);

            #region Example
            StructurePerson strX = new StructurePerson();
            strX.LastName  = "Bejaoui";
            strX.FirstName = "Bechir";
            StructurePerson strY = new StructurePerson();
            strY.LastName  = "Bejaoui";
            strY.FirstName = "Bechir";

            if (strX.Equals(strY))
            {
                Console.WriteLine("strX = strY");
            }
            else
            {
                Console.WriteLine("strX != strY");
            }//This code displays strX = strY
            ClassPerson clsX = new ClassPerson();
            clsX.LastName  = "Bejaoui";
            clsX.FirstName = "Bechir";
            ClassPerson clsY = new ClassPerson();
            clsY.LastName  = "Bejaoui";
            clsY.FirstName = "Bechir";
            if (clsX.Equals(clsY))
            {
                Console.WriteLine("clsX = clsY");
            }
            else
            {
                Console.WriteLine("clsX != clsY");
            }//This code displays clsX != clsY
            #endregion


            Console.ReadKey();
        }
예제 #2
0
        /*
         * :: Differences :: 
         * 1. A struct is a value type where as class is a referance type.
         * 2. All the differences apply to value type and reference type are applicable here also.
         * 3. Structs are stored in stack and classes in heap.
         * 4. Value types holds there value in the memory where they created whereas reference type hold a reference to an object in memory.
         * 5. Value types are distroyed immediately after the scope is lost, whereas for reference types only 
         * the reference variable is distroyed after the scope is lost. The object is later distroyed by garbage collector.
         * 6. When you copy a struct into another struct, a new copy of struct created and modification at one struct will not affect value contained by other.
         * 7. structs can't have destructors where as class can have.
         * 8. structs can't have explicit parameter less constructor where as a class can. 
         * 9. structs can't inherit from another class where as a class can. Both structs and classes can inherit from an interface.
         * 10. A class or struct cannot inherit from another struct. Structs are sealed types. 
         * 11. Since struct does not support inheritance, access modifier of a member of a struct cannot be protected or protected internal.

         */
        static void Main(string[] args)
        {
            #region Value types holds there value in the memory where they created whereas reference type hold a reference to an object in memory.
            // Value types are distroyed immediately after the scope is lost, whereas for reference types only 
            // the reference variable is distroyed after the scope is lost. The object is later distroyed by garbage collector.
            int i = 1;

            if (i == 1)
            {
                int j = 10;
                Customer c = new Customer();
                c.ID = 101;
                c.Name = "Anand";
            }
            #endregion

            #region When you copy a struct into another struct, a new copy of struct created and modification at one struct will NOT affect value contained by other.

            int a = 10;
            int b = a;
            b = b + 1;
            a = a + 4; // This operation on a will not be reflected at b.
            Console.WriteLine("a = {0}, b = {1}", a, b);

            #endregion

            #region When you copy a class into another class, a only get a copy of reference variable, Both the variables point to the same onject on the heap and modification at one reference variable will affect value contained by other.

            Customer c1 = new Customer();
            c1.ID = 100;
            c1.Name = "Anand";
            Console.WriteLine("c1.ID = {0}, c1.Name = {1}", c1.ID, c1.Name);

            Customer c2 = c1;
            c2.Name = "Sahil"; // This operation on c2.Name will be reflected at c1.Name.
            Console.WriteLine("c1.ID = {0}, c1.Name = {1}", c1.ID, c1.Name);

            #endregion

            //Struct_1 s = new Struct_1();
            //s.Equals(s);

            #region Example
            StructurePerson strX = new StructurePerson();
            strX.LastName = "Bejaoui";
            strX.FirstName = "Bechir";
            StructurePerson strY = new StructurePerson();
            strY.LastName = "Bejaoui";
            strY.FirstName = "Bechir";

            if (strX.Equals(strY))
            {
                Console.WriteLine("strX = strY");
            }
            else
            {
                Console.WriteLine("strX != strY");
            }//This code displays strX = strY
            ClassPerson clsX = new ClassPerson();
            clsX.LastName = "Bejaoui";
            clsX.FirstName = "Bechir";
            ClassPerson clsY = new ClassPerson();
            clsY.LastName = "Bejaoui";
            clsY.FirstName = "Bechir";
            if (clsX.Equals(clsY))
            {
                Console.WriteLine("clsX = clsY");
            }
            else
            {
                Console.WriteLine("clsX != clsY");
            }//This code displays clsX != clsY
            #endregion


            Console.ReadKey();
        }