Instance, Class & Reference Variables in C#

Understanding Instance, Class & Reference Variables

Variables in C# are categorized into instance variables, class (static) variables, and reference variables based on their scope, behavior, and memory allocation.

Instance Variables

An instance variable is a non-static field that belongs to an object (instance) of a class. Each object has its own copy of instance variables.

Example of Instance Variables:

public class Car
{
    // Instance variables (non-static fields)
    public string brand;
    public int speed;

    // Constructor
    public Car(string brand, int speed)
    {
        this.brand = brand;
        this.speed = speed;
    }

    public void ShowDetails()
    {
        Console.WriteLine($"Brand: {brand}, Speed: {speed} km/h");
    }
}

// Usage
Car car1 = new Car("Toyota", 100);
Car car2 = new Car("Honda", 120);

car1.ShowDetails();  // Output: Brand: Toyota, Speed: 100 km/h
car2.ShowDetails();  // Output: Brand: Honda, Speed: 120 km/h
        

Each object has a unique copy of instance variables. Changes to one instance do not affect another.

Class (Static) Variables

A class variable (also known as a static variable) is shared across all instances of the class. It belongs to the class rather than a specific instance.

Example of Static Variables:

public class BankAccount
{
    // Static variable shared by all instances
    public static double interestRate = 5.0;
    public string accountHolder;

    public BankAccount(string accountHolder)
    {
        this.accountHolder = accountHolder;
    }

    public void ShowInterestRate()
    {
        Console.WriteLine($"{accountHolder} - Interest Rate: {interestRate}%");
    }
}

// Usage
BankAccount acc1 = new BankAccount("Alice");
BankAccount acc2 = new BankAccount("Bob");

acc1.ShowInterestRate(); // Output: Alice - Interest Rate: 5%
acc2.ShowInterestRate(); // Output: Bob - Interest Rate: 5%

BankAccount.interestRate = 6.5; // Modifying static variable

acc1.ShowInterestRate(); // Output: Alice - Interest Rate: 6.5%
acc2.ShowInterestRate(); // Output: Bob - Interest Rate: 6.5%
        

Static variables are shared among all instances. Changing it in one place affects all instances.

Reference Variables

A reference variable holds the memory address of an object rather than the object itself. Assigning a reference variable to another variable makes both point to the same object.

Example of Reference Variables:

public class Person
{
    public string Name;

    public Person(string name)
    {
        Name = name;
    }
}

// Usage
Person person1 = new Person("Alice");
Person person2 = person1; // Both reference the same object

person2.Name = "Bob";

Console.WriteLine(person1.Name); // Output: Bob
        

Since person1 and person2 reference the same object, modifying one affects the other.

Differences Between Instance, Class & Reference Variables

Feature Instance Variable Class (Static) Variable Reference Variable
Belongs To Each object The class Memory address of an object
Storage Heap Class-level (Static memory) Heap (Reference points to memory location)
Effect on Copy Each instance has its own copy Shared among all instances Multiple references point to the same object

Key Points to Remember

  • Instance variables belong to individual objects and are not shared.
  • Class (static) variables belong to the class and are shared across all objects.
  • Reference variables hold memory addresses and can point to the same object.
  • Understanding these concepts is crucial for effective memory management in C#.