Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are reference data types in C#?
The reference data types in C# do not store the actual data directly in a variable, but instead contain a reference (or pointer) to the memory location where the data is stored. When you assign a reference type variable to another variable, both variables point to the same object in memory.
In C#, the following are the built-in reference types −
Object Type
The object type is the ultimate base class for all data types in C# Common Type System (CTS). Object variables can be assigned values of any other types, whether value types, reference types, predefined, or user-defined types. When a value type is assigned to an object, it is boxed.
Syntax
object variableName = value;
Example
using System;
class Program {
public static void Main() {
object obj1 = 250; // boxing int to object
object obj2 = "Hello"; // string reference
object obj3 = 3.14; // boxing double to object
Console.WriteLine("obj1: " + obj1 + " (Type: " + obj1.GetType() + ")");
Console.WriteLine("obj2: " + obj2 + " (Type: " + obj2.GetType() + ")");
Console.WriteLine("obj3: " + obj3 + " (Type: " + obj3.GetType() + ")");
}
}
The output of the above code is −
obj1: 250 (Type: System.Int32) obj2: Hello (Type: System.String) obj3: 3.14 (Type: System.Double)
Dynamic Type
The dynamic type can store any type of value. Type checking for dynamic variables takes place at run-time rather than compile-time. This means errors related to type mismatches will only be discovered when the program runs.
Syntax
dynamic variableName = value;
Example
using System;
class Program {
public static void Main() {
dynamic d = 100;
Console.WriteLine("d as int: " + d);
d = "Now I'm a string";
Console.WriteLine("d as string: " + d);
d = 3.14159;
Console.WriteLine("d as double: " + d);
}
}
The output of the above code is −
d as int: 100 d as string: Now I'm a string d as double: 3.14159
String Type
The string type allows you to assign string values to a variable. The string type is an alias for the System.String class and is derived from the object type. Strings in C# are immutable, meaning their values cannot be changed after creation.
Syntax
string variableName = "string value";
Example
using System;
class Program {
public static void Main() {
string str1 = "Welcome!";
string str2 = "C# Programming";
string str3 = str1 + " to " + str2;
Console.WriteLine("str1: " + str1);
Console.WriteLine("str2: " + str2);
Console.WriteLine("str3: " + str3);
Console.WriteLine("Length of str3: " + str3.Length);
}
}
The output of the above code is −
str1: Welcome! str2: C# Programming str3: Welcome! to C# Programming Length of str3: 28
Reference vs Value Types Comparison
| Reference Types | Value Types |
|---|---|
| Store reference to memory location | Store actual data directly |
| Allocated on heap memory | Allocated on stack memory |
| Can be null | Cannot be null (unless nullable) |
| Examples: object, string, dynamic, arrays | Examples: int, float, bool, char |
Conclusion
Reference data types in C# store references to objects rather than the actual data. The main built-in reference types are object (base class for all types), dynamic (runtime type checking), and string (immutable text data). Understanding the difference between reference and value types is crucial for memory management and avoiding common programming errors.
