Instance, Class & Reference Variables in C#

What are Instance, Class, and Reference Variables?

In C#, variables can be categorized into three main types: Instance variables, Class variables (also called static variables), and Reference variables. Understanding the differences between these variables is crucial for object-oriented programming.

Instance Variables

Instance variables are declared within a class but outside any methods. They are specific to an instance of the class, meaning each object of the class has its own copy of instance variables.

Example of Instance Variables:

public class Car
{
    // Instance variables
    public string model;
    public int year;

    public Car(string model, int year)
    {
        this.model = model;
        this.year = year;
    }

    public void DisplayInfo()
    {
        Console.WriteLine($"Model: {model}, Year: {year}");
    }
}

// Usage
Car car1 = new Car("Toyota", 2022);
Car car2 = new Car("Honda", 2021);
car1.DisplayInfo();
car2.DisplayInfo();
        

In this example, model and year are instance variables. Each object (car1 and car2) has its own copy of these variables.

Class Variables (Static Variables)

Class variables, also known as static variables, are shared among all instances of a class. These variables are declared using the static keyword and belong to the class rather than any specific instance.

Example of Class Variables:

public class Car
{
    // Static variable
    public static int numberOfCars;

    public string model;
    public int year;

    public Car(string model, int year)
    {
        this.model = model;
        this.year = year;
        numberOfCars++;  // Increment static variable
    }

    public static void DisplayTotalCars()
    {
        Console.WriteLine($"Total number of cars: {numberOfCars}");
    }
}

// Usage
Car car1 = new Car("Toyota", 2022);
Car car2 = new Car("Honda", 2021);
Car.DisplayTotalCars();
        

In this example, numberOfCars is a static (class) variable, which is shared among all instances of the Car class. The static method DisplayTotalCars shows the total number of cars created.

Reference Variables

A reference variable holds the reference (address in memory) to an object rather than the actual object data. Reference variables are used to manipulate objects in C#.

Example of Reference Variables:

Car car1 = new Car("Toyota", 2022);
Car car2 = car1;  // car2 is a reference variable, it points to the same object as car1
car2.model = "Honda";  // This changes car1 as well, since both reference the same object
Console.WriteLine(car1.model);  // Output: Honda
        

In this example, car2 is a reference variable pointing to the same object as car1. Changes made to car2 also affect car1 because they both reference the same memory location.

Key Points to Remember

  • Instance variables are unique to each object, while class variables (static) are shared among all objects of the class.
  • Reference variables store the address of an object and allow manipulation of the object's data.
  • Static variables and methods belong to the class and can be accessed without creating an instance.
  • Changes made through a reference variable affect the original object.