C# tutorials > Core C# Fundamentals > Basics and Syntax > What are value types and reference types?
What are value types and reference types?
Core Difference: Storage
Value Types
When `y` is assigned the value of `x`, a new memory location is allocated for `y`, and the value of `x` is copied into it. Therefore, modifying `y` has no effect on `x`.
csharp
int x = 10;
int y = x; // y gets a copy of x's value (10)
y = 20; // Changing y does not affect x
Console.WriteLine($"x: {x}, y: {y}"); // Output: x: 10, y: 20
Reference Types
In this example, `obj2` is assigned the *reference* of `obj1`. Both variables now point to the same object in memory. Modifying `obj2`'s `Value` property directly modifies the object that both `obj1` and `obj2` are referencing.
csharp
class MyClass
{
public int Value { get; set; }
}
MyClass obj1 = new MyClass { Value = 10 };
MyClass obj2 = obj1; // obj2 now references the same object as obj1
obj2.Value = 20; // Changing obj2 also changes obj1
Console.WriteLine($"obj1.Value: {obj1.Value}, obj2.Value: {obj2.Value}"); // Output: obj1.Value: 20, obj2.Value: 20
Concepts Behind the Snippet (Garbage Collection)
Real-Life Use Case: Modifying Shared Data
csharp
using System.Collections.Generic;
public class ShoppingCart
{
private List<string> items = new List<string>();
public void AddItem(string item)
{
items.Add(item);
}
public List<string> GetItems()
{
//Return a new list instead of the internal one to avoid modifications
return new List<string>(items);
}
public override string ToString()
{
return string.Join(", ", items);
}
}
public class Example
{
public static void Main(string[] args)
{
ShoppingCart cart1 = new ShoppingCart();
cart1.AddItem("Laptop");
cart1.AddItem("Mouse");
ShoppingCart cart2 = cart1; // Both point to the same cart
cart2.AddItem("Keyboard");
Console.WriteLine("Cart 1: " + cart1);
Console.WriteLine("Cart 2: " + cart2);
// Output: Cart 1: Laptop, Mouse, Keyboard
// Output: Cart 2: Laptop, Mouse, Keyboard
}
}
Best Practices
Interview Tip
When to Use Them
Memory Footprint
Alternatives: Immutable Reference Types
Pros and Cons - Value Types
Cons:
Pros and Cons - Reference Types
Cons:
FAQ
-
What is boxing and unboxing?
Boxing is the process of converting a value type to a reference type (specifically, to an `object`). Unboxing is the reverse process – converting a reference type (that was previously boxed) back to a value type. Boxing and unboxing can have performance implications. -
When should I use a struct instead of a class?
Use a struct when you need a lightweight, immutable data structure to represent a single value. Structs are value types, so they are copied when assigned or passed as arguments. This makes them ideal for representing simple data structures like points or colors. Avoid structs for complex objects with significant data or behavior. -
What is the `Nullable
` type?
`Nullable` (or `T?` shorthand) allows you to represent a value type that can also be null. This is useful when you need to indicate that a value is missing or undefined. For example, `int? age = null;`