C# Reference type assignment VS value type assignment

后端 未结 6 1378
自闭症患者
自闭症患者 2020-12-06 22:27

I understand the theoretical concept that assigning one reference type variable to another, only the reference is copied, not the object. assigning one value type variable t

相关标签:
6条回答
  • 2020-12-06 23:04

    //Reference Type

            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            sb2 = sb1;
            sb1.Append("hello");
            sb1.Append("world");
            Console.WriteLine(sb2);
            // Output Hello World
    
    
            // value Type
            int x=100;            
            int y = x;
            x = 30;            
            Console.WriteLine(y);
    
            // Out put 100 instead of 30
    
    0 讨论(0)
  • 2020-12-06 23:07

    The first code snippet contains Employee which is a class and in the second one, Employee is a struct

    0 讨论(0)
  • 2020-12-06 23:16

    One is a structure and the other is a class. This seems like an overly complicated example involving more than just value and reference differences but the differences between classes and structs as well.

    When one struct is assigned to another a copy is made.
    When one class is assigned to another only the reference changes.

    0 讨论(0)
  • 2020-12-06 23:18

    Well, the obvious difference is that with the class example, it appears both joe and bob changed in the last part there, to the same value.

    In the struct example, they keep their separate values, simply because each variable is a whole struct value by itself, not just a reference to a common object in memory somewhere.

    The main code-wise difference being the type you use, class or struct, this dictates whether you're creating a reference type or a value type.

    0 讨论(0)
  • 2020-12-06 23:18

    To see the difference more clearly, try something like

    joe.Inches = 71;
    bob.Inches = 59;
    ...
    

    // assign joe reference value to bob variable

    bob = joe;
    joe.Inches = 62;
    
    // write bob and joe
    

    And do something similar in the reference-type example.

    You will be able to demonstrate that in the second examples there are two different instances of Height, while in the first example there is only one (shared) instance of Employee.

    0 讨论(0)
  • 2020-12-06 23:20

    On my system, those two code blocks produce the following output:

    Original Employee Values:

    joe = Joe
    bob = Bob
    
    Values After Reference Assignment:
    joe = Joe
    bob = Joe
    
    Values After Changing One Instance:
    joe = Bobbi Jo
    bob = Bobbi Jo
    

    ...and...

    Original Height            Values:
    joe = 71
    bob = 59
    
    Values After            Value Assignment:
    joe = 71
    bob = 71
    
    Values After            Changing One Instance:
    joe = 65
    bob = 71
    

    The difference seems self-evident. In the first sample, changing one of the values affects both references, because they are both referencing the same underlying object. In the second sample, changing one of the objects affects only that object, because when value types are copied, each object receives it own private copy. When you say bob = joe; in the second sample, you;re not assigning a reference (the comment is misleading). What you're doing is creating a new instance of the Height struct, copying all the values from joe and storing the new object as bob.

    0 讨论(0)
提交回复
热议问题