In C#, what does "Customer cust = new Customer();" do?

Learn in c#, what does "customer cust = new customer();" do? with practical examples, diagrams, and best practices. Covers c# development techniques with visual explanations.

Understanding 'Customer cust = new Customer();' in C#

Hero image for In C#, what does "Customer cust = new Customer();" do?

Explore the fundamental C# statement Customer cust = new Customer(); to understand object instantiation, memory allocation, and constructor execution.

In C#, the line Customer cust = new Customer(); is a cornerstone of object-oriented programming. It's a seemingly simple statement that encapsulates several crucial concepts: declaring a reference variable, instantiating an object, allocating memory, and executing a constructor. Understanding each part of this statement is fundamental to writing effective and robust C# applications.

Deconstructing the Statement

Let's break down the statement Customer cust = new Customer(); into its individual components to understand what each part signifies and how they work together to create an object.

flowchart LR
    A["Customer cust"] --> B["Declaration: Reference Variable"];
    B --> C["=": Assignment Operator];
    C --> D["new Customer()": Object Instantiation];
    D --> E["Memory Allocation"];
    E --> F["Constructor Execution"];
    F --> G["Return Reference"];
    G --> B;
    style A fill:#f9f,stroke:#333,stroke-width:2px
    style D fill:#bbf,stroke:#333,stroke-width:2px

Flowchart illustrating the steps involved in object instantiation

1. Customer cust - Declaration of a Reference Variable

The first part, Customer cust, declares a variable named cust. This variable is not the object itself, but rather a reference (or pointer) that can hold the memory address of an object of type Customer. Think of it like an empty box labeled 'Customer' that is ready to hold a specific customer's details. At this point, cust is initialized to null, meaning it doesn't point to any object in memory yet.

Customer cust; // Declares a reference variable 'cust' of type Customer. It is currently null.

Declaring a reference variable in C#

2. new Customer() - Object Instantiation and Memory Allocation

The new keyword is crucial. It performs two primary actions:

  1. Memory Allocation: It allocates enough memory on the heap to store a new Customer object. The heap is a region of memory used for dynamic memory allocation, where objects live until they are no longer referenced and are eventually collected by the garbage collector.
  2. Constructor Invocation: After allocating memory, the new keyword calls the constructor of the Customer class. A constructor is a special method that initializes the newly created object. If no explicit constructor is defined in the Customer class, C# provides a default parameterless constructor that initializes all fields to their default values (e.g., 0 for numeric types, false for booleans, null for reference types).

The new Customer() expression evaluates to the memory address (a reference) of the newly created and initialized Customer object.

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }

    // Default constructor (implicitly provided if not defined, or explicitly defined here)
    public Customer()
    {
        Id = 0; // Explicitly initializing, though default would be 0
        Name = ""; // Explicitly initializing, though default would be null
        Console.WriteLine("Customer object created!");
    }
}

// In another part of your code:
Customer newCustomer = new Customer(); // This calls the constructor above.

Example of a Customer class with a constructor

3. = - The Assignment Operator

Finally, the assignment operator = takes the reference (memory address) returned by new Customer() and assigns it to the cust variable. Now, cust is no longer null; it points to the newly created Customer object in memory. Any operations performed on cust will now affect that specific Customer object.

Customer cust = new Customer(); // Combines declaration, instantiation, and assignment

cust.Id = 101;
cust.Name = "Alice Smith";

Console.WriteLine($"Customer ID: {cust.Id}, Name: {cust.Name}");

Assigning the object reference to the variable and accessing its properties