C# Reference type assignment VS value type assignment

后端 未结 6 1379
自闭症患者
自闭症患者 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
    

提交回复
热议问题