Two reference type variables may refer (or point) to the same object

image_pdfimage_print

   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

/*
    Refs1.cs - Shows that two reference type variables may refer (or point)
               to the same object. Changing the object using one variable
               changes the object for the other variable.

            Compile this program with the following command line:
                csc refs1.cs
 */
namespace nsReference
{
    using System;
    public class Refs1
    {
        static public void Main ()
        {
            clsClass first = new clsClass (42);
            clsClass second = first;
            second.m_Var /= 2;
            Console.WriteLine ("first.m_Var = " + first.m_Var);
        }
    }
    class clsClass
    {
        public clsClass (int var)
        {
            m_Var = var;
        }
        public int m_Var;
    }
}