Differences between Value type and reference types in dotnet framework
Loading
Differences between Value type and reference types in dotnet framework
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Jun 5, 2025, 1:25 AM EDT
Value types and Reference types are two fundamental categories of data types, and they behave differently in terms of memory allocation, storage, and assignment.
Code Example:
// Value type int a = 10; int b = a; b = 20; // a is still 10 // Reference type class MyClass { public int Value; } MyClass x = new MyClass { Value = 10 }; MyClass y = x; y.Value = 20; // x.Value is now 20Aman GuptaRank #193 · 12.4K reputationPosted Jun 5, 2025, 2:09 AM EDT
Hi Kiran,
Value Types and Reference Types differ primarily in how they store data and how that data is accessed. In the .NET Framework, Value Types and Reference Types have similar fundamental differences as in Java.
Example: Value Type Behavior
int a = 5; int b = a; b = 10; Console.WriteLine(a); // Output: 5 (original unchanged)Example: Reference Type Behavior
class Person { public string Name; } Person p1 = new Person(); p1.Name = "Alice"; Person p2 = p1; p2.Name = "Bob"; Console.WriteLine(p1.Name); // Output: "Bob" (shared reference)