Reference type holds the reference of the value, that is when we declare a reference type, then the compiler stores the value in the managed heap. However, the address of the memory is stored in the stack.
C# also provides the following built-in reference types:
Output :
Storage :
Lets assign reference type stu to stu2 as follows. stu2 = stu; As soon as compiler recognizes the code, it updates the memory address of emp2. (e.g. 0x000001 , which is memory address of stu as shown in the image). That means, both the reference types stu and stu2 point to same memory location. Any change to any of the variable affects the other, because they are both pointing to the same location. The output value of stu2.Name will be “Sam”. as below... If we change the variable name to "Amit", the output for both the object name will be Amit...
C# also provides the following built-in reference types:
- dynamic
- object
- string
- class
- interface
- delegate
- record
Example 1 - Declare one object and its memory structure
Consider the simple program for reference type with Student object and has it's properties as Name and Age.
Consider the simple program for reference type with Student object and has it's properties as Name and Age.
public class Student{ public string Name {get; set;} public int Age {get; set;} } public class program { static void Main(string[] args) { Student stu = new Student(); stu.Name = "Sam"; stu.Age =23;
Console.WriteLine("student1 : " + stu.Name.ToString());
}
}
Output :
student1 : Sam
Storage
In this example , the variable "stu" is declared as a reference type,
hence the compiler allocates memory from heap and stores the values.
The memory address 0x000001 of the reference type is stored in the stack as explained in the diagram below
Example 2 - Declare 2 separate references
In example 2, we have declared two reference types stu and stu2. Both these reference types have been allocated memory in managed heap. The memory addresses of the reference types stu and stu2 have been stored in the stack as shown in the image below
Code :
hence the compiler allocates memory from heap and stores the values.
The memory address 0x000001 of the reference type is stored in the stack as explained in the diagram below
Example 2 - Declare 2 separate references
In example 2, we have declared two reference types stu and stu2. Both these reference types have been allocated memory in managed heap. The memory addresses of the reference types stu and stu2 have been stored in the stack as shown in the image below
Code :
public class program { static void Main(string[] args) { Student stu = new Student(); stu.Name = "Sam"; stu.Age = 22; Student stu2 = new Student(); stu2.Name = "Niran"; stu2.Age = 26; Console.WriteLine("Both stu & stu2 has separate location."); Console.WriteLine("student1 : " + stu.Name.ToString()); Console.WriteLine("student2 : " + stu2.Name.ToString()); } } public class Student { public string Name { get; set; } public int Age { get; set; } }
Output :
Both stu & stu2 has separate location.student1 : Samstudent2 : Niran
Storage
In example 2, we have declared two reference types stu and stu2. Both these reference types have been allocated memory in managed heap. The memory addresses of the reference types stu and stu2 have been stored in the stack as shown in the image above.
Example 3 - Variable Assignment
If we assign one reference type variable to another reference type then the memory address will be copied and both variables will point to same memory location. So any value change to any of the variable would affects to the other as well.
Code :
Example 3 - Variable Assignment
If we assign one reference type variable to another reference type then the memory address will be copied and both variables will point to same memory location. So any value change to any of the variable would affects to the other as well.
Code :
public class program { static void Main(string[] args) { Student stu = new Student(); stu.Name = "Sam"; stu.Age = 22; Student stu2 = new Student(); stu2.Name = "Niran"; stu2.Age = 26; Console.WriteLine("Before assigning, stu & stu2 has separate location."); Console.WriteLine("student1 : " + stu.Name.ToString()); Console.WriteLine("student2 : " + stu2.Name.ToString()); Console.WriteLine("**************************"); //Assign stu to stu2 stu2 = stu; Console.WriteLine("After assigning, stu & stu2 points to same location."); Console.WriteLine("student1 : " + stu.Name.ToString()); Console.WriteLine("student2 : " + stu2.Name.ToString()); } } public class Student { public string Name { get; set; } public int Age { get; set; } }
Output :
Before assigning, stu & stu2 has separate location.student1 : Samstudent2 : Niran**************************After assigning, stu & stu2 points to same location.student1 : Samstudent2 : Sam
Storage :
Lets assign reference type stu to stu2 as follows. stu2 = stu; As soon as compiler recognizes the code, it updates the memory address of emp2. (e.g. 0x000001 , which is memory address of stu as shown in the image). That means, both the reference types stu and stu2 point to same memory location. Any change to any of the variable affects the other, because they are both pointing to the same location. The output value of stu2.Name will be “Sam”. as below... If we change the variable name to "Amit", the output for both the object name will be Amit...