Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Reference Types and References

Tags:

c#

.net

clr

I am reading following blog by Eric Lippert: The truth about Value types

In this, he mentions there are 3 kinds of values in the opening:

  1. Instance of Value types

  2. Instance of Reference types

  3. References

    It is incomplete. What about references? References are neither value types nor instances of reference types, but they are values..

So, in the following example:

int i = 10;
string s = "Hello"

First is instance of value type and second is instance of reference type. So, what is the third type, References and how do we obtain that?

like image 271
Chander Shivdasani Avatar asked Apr 20 '26 20:04

Chander Shivdasani


1 Answers

So, what is the third type, References and how do we obtain that?

The variable s is a variable which holds the value of the reference. This value is a reference to a string (with a value of "Hello") in memory.

To make this more clear, say you have:

 string s1 = "Hello";
 string s2 = s1;

In this case, s1 and s2 are both variables that are each a reference to the same reference type instance (the string). There is only a single actual string instance (the reference type) involved here, but there are two references to that instance.

like image 54
Reed Copsey Avatar answered Apr 22 '26 14:04

Reed Copsey