What is the difference between a reference type and value type in c#?

后端 未结 14 2418
谎友^
谎友^ 2020-11-21 05:13

Some guy asked me this question couple of months ago and I couldn\'t explain it in detail. What is the difference between a reference type and a value type in C#?

I

14条回答
  •  甜味超标
    2020-11-21 05:58

    Suppose v is a value-type expression/variable, and r is a reference-type expression/variable

        x = v  
        update(v)  //x will not change value. x stores the old value of v
    
        x = r 
        update(r)  //x now refers to the updated r. x only stored a link to r, 
                   //and r can change but the link to it doesn't .
    

    So, a value-type variable stores the actual value (5, or "h"). A reference-type varaible only stores a link to a metaphorical box where the value is.

提交回复
热议问题