Two reference type variables may refer (or point) to the same object : Variable Definition « Language Basics « C# / C Sharp






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

Two reference type variables may refer (or point)
               to the same object
/*
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;
    }
}

           
       








Related examples in the same category

1.Declaring a variable.
2.Initializing a variable.
3.Variable default nameVariable default name
4.Heap and Stack Memory
5.Create a 4-bit type called NybbleCreate a 4-bit type called Nybble
6.Use new with a value typeUse new with a value type
7.Init variableInit variable
8.An attempt to reference an uninitialized variable
9.Illustrates variable scope
10.Demonstrate the use of readonly variablesDemonstrate the use of readonly variables
11.Uninitialized Values
12.Int, float, double, decimalInt, float, double, decimal
13.Demonstrate dynamic initializationDemonstrate dynamic initialization
14.Demonstrate block scopeDemonstrate block scope
15.Demonstrate lifetime of a variableDemonstrate lifetime of a variable
16.This program attempts to declared a variable in an inner scope
17.Demonstrate castingDemonstrate casting
18.A promotion surpriseA promotion surprise
19.Using casts in an expressionUsing casts in an expression
20.Create an implication operator in C#Create an implication operator in C#
21.declaring a reference type variable and creating an object the variable will reference
22.Definite Assignment and ArraysDefinite Assignment and Arrays
23.Variable Scoping and Definite Assignment:Definite AssignmentVariable Scoping and Definite Assignment:Definite Assignment