Class internal structure

Can someone explain what happens when the reference type is defined inside the value type. I am writing the following code:

namespace ClassInsideStruct
{
    class ClassInsideStruct
    {
        static void Main(string[] args)
        {

            ValueType ObjVal = new ValueType(10);
            ObjVal.Display();

            ValueType.ReferenceType ObjValRef = new ValueType.ReferenceType(10);
            ObjValRef.Display();

            Test(ObjVal, ObjValRef);

            ObjVal.Display();
            ObjValRef.Display();

            Console.ReadKey();

        }

        private static void Test(ValueType v, ValueType.ReferenceType r)
        {
            v.SValue = 50;
            r.RValue = 50;
        }

    }

    struct ValueType
    {

        int StructNum;
        ReferenceType ObjRef;

        public ValueType(int i)
        {
            StructNum = i;
            ObjRef = new ReferenceType(i);
        }

        public int SValue
        {
            get { return StructNum; }
            set
            {
                StructNum = value;
                ObjRef.RValue = value;
            }
        }

        public void Display()
        {
            Console.WriteLine("ValueType: " + StructNum);
            Console.Write("ReferenceType Inside ValueType Instance: ");
            ObjRef.Display();
        }

        public class ReferenceType
        {

            int ClassNum;

            public ReferenceType(int i)
            {
                ClassNum = i;
            }

            public void Display()
            {
                Console.WriteLine("Reference Type: " + ClassNum);
            }

            public int RValue
            {
                get { return ClassNum; }
                set { ClassNum = value; }
            }

        }

    }

}

What outputs:

ValueType: 10
ReferenceType Inside ValueType Instance: Reference Type: 10
Reference Type: 10
ValueType: 10
ReferenceType Inside ValueType Instance: Reference Type: 50
Reference Type: 50

I am interested to find out by calling the method Test(ObjVal, ObjValRef)how the values ​​are ReferenceTypechanged to 50, which are inside ValueType, the value of which is not changed?

+5
source share
5 answers

, , , , . , , . , . , , , . , , .

+2

, - . , .

, . , ( ).

i. . , , .

+2

, . , , .

+1

, . .

+1

, , , // , . :

static void Main(string[] args)
{

    ValueType ObjVal = new ValueType(10);
    ObjVal.Display();

    ValueType.ReferenceType ObjValRef = new ValueType.ReferenceType(10);
    ObjValRef.Display();

    //call to Test(ObjVal, ObjValRef); replaced by the following 4 lines
    ValueType v = ObjVal;
    ReferenceType r = ObjValRef;
    v.SValue = 50;
    r.RValue = 50;

    ObjVal.Display();
    ObjValRef.Display();

    Console.ReadKey();

}

, . ValueType v = ObjVal;, , , v . ObjVal.

ReferenceType r = ObjValRef; . , : ObjValRef r, . , ValueType.ReferenceType(10);

, , , .

oh, by by.. - . 32- , .

, . r = null; "" ObjValRef, r ObjValRef, ObjValRef. , , .

( - , , " " ), - , . , , , " ". .

-1
source

All Articles