Uniformity of objects in .NET.

I feel rather ignorant asking about this, but can someone explain to me why this is happening?

class MyClass {public int i {get; set; }}
class program
{
    static void Main (string [] args)
    {
        MyClass a = new MyClass ();
        MyClass b = new MyClass ();

                bi = 2;
        a = b;
        ai = 1;

        Console.Write (bi + "\ n"); // Outputs 1
    }
}

It makes sense to me if I use pointers and all these wonderful things, but I got the impression that with C #, that "b" would remain independent of "a".

Am I just using some terribly bad practice? Can someone point me to something that explains why this is so in C #?

Thank.

+3
6

, :

a = b;

, b a , , , b a.

.Net : ( , ). , , , , :

  • . == ( ), .Equals() , .Equals() ( GetHashCode()) , .
  • ( , )
+10

. , ValueType.

,

a = b;

a, b.

+4

, , 4 , .

  • №1 MyClass
  • №2 MyClass
  • MyClass a
  • MyClass b

# 1, b # 2. a=b;. a, b # 1. , b.i, # 1, if i. # 2.

+2

. "a" MyClass, "b". "a = b", , , "a" , "b".

+1

a b - , a = b a , b ( , a, ). , a.i, , b, , .

0

.NET . , .

, = operator (), a alsto b. orther, .

If you need to duplicate an object, you need to write the code yourself. But this is another story.

0
source

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


All Articles