Assigning an object to another creates a copy?

I tried with the code below, I got the output as 1000. I heard that the assigning object should share the link instead of copying the entire memory of the object. Here the result is different. Can someone help.

public aaaaa ad = new aaaaa();

static void Main(string[] args)
{
    Program p = new Program();
    p.fun1();
    p.fun2();
}

public void fun1()
{
    using(smallclass s = new smallclass())
    {
        s.j = 1000;
        ad.fun1(s);
    }
}

public void fun2()
{
    ad.fun2();
}
public class aaaaa
{
    public smallclass h = new smallclass();
    public void fun1(smallclass d)
    {
        h = d;
    }
    public void fun2()
    {
        Console.WriteLine(h.j);
    }
}

public class smallclass:IDisposable
{
    public int j = 9;

    public void Dispose()
    {
        GC.SuppressFinalize(this);
    }
}

Update: I expect an object reference to be thrown because the reference memory is located in p.fun1 ();

+4
source share
4 answers

Here is a simple example of how to cope with work

using System;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static smallclass objA = new smallclass();
        private static smallclass objB = new smallclass();

        private static void Main(string[] args)
        {
            showValues();

            objA.value = 1000;

            showValues();

            objB = objA;

            showValues();

            objA.value = 1055;

            showValues();
        }

        private static void showValues()
        {
            Console.WriteLine("objA.value: " + objA.value);
            Console.WriteLine("objB.value: " + objB.value);

            Console.ReadLine();
        }
    }

    internal class smallclass : IDisposable
    {
        public int value = 0;

        public void Dispose()
        {
            //Here you can remove eventHandlers
            //or do some other stuff before the GC will play with it
        }
    }
}

as you can see

  • first we create 2 objects objAand objB
    than we show the values, as expected, they are 0

  • objA 1000 objA a 1000, objB 0

  • objA objB objB 1000

  • objA 1055 objB objB , objA

,

aaaaa class :

public class aaaaa
{
    public WeakReference<smallclass> h;
    public void fun1(smallclass d)
    {
        h = new WeakReference<smallclass>(d);
    }
    public void fun2()
    {
        smallclass k;
        if(h.TryGetTarget(out k))
        Console.WriteLine(k.j);
        else
            Console.WriteLine("ERROR ERRROR ERROR");
    }
}

static void Main(string[] args) :

    static void Main(string[] args)
    {
        Program p = new Program();
        p.fun1();
        GC.Collect();
        p.fun2();

        Console.Read();
    }

Ok

WeakReference<T> ( WeakReference) GC , StrongReference,

GC.Collect() , GC ( )

, IDisposable GC , (AFAIK), , , , .

+2

, assingning "" , ... , . .

.

#, - .

+2

, , , , , "null" "object # 24601" [ - ], , :

  • , .

  • - ,

myCar - , myCar.Color = CarColors.Blue myCar. , myCar [.] " № 8675309", Color # 8675309. , otherCar " # 90210", otherCar=myCar # 8675309 # 90210, "90210", otherCar "8675309".

, , , , , , , , : WeakReference ; , , , , WeakReference . , , , . , , , Finalize . Finalize , . , .

+1

GC.SuppressFinalize GC.Collect() dispose, .. 1000 .

I assume that since it contains another reference (the h variable), GC will not free memory, even if we choose it explicitly.

Thus, we can very well transfer and assign objects regardless of the selected (new) object emerging from the scope.

Please correct me if I am wrong.

0
source

Source: https://habr.com/ru/post/1526874/


All Articles