Skip to main content

Command Palette

Search for a command to run...

Value , Reference & Nullable Types

Knowing the difference between this helps better decision making when writing code.

Updated
4 min read
Value , Reference  & Nullable Types

1. Value Types

Description: A value type is like a piece of paper with a number or value written on it. When you give it to someone (or assign it to another variable), you’re giving them a copy of that paper. Changing the copy doesn’t affect the original. Value types store the actual data directly and are typically simple, like numbers or true/false values.

Key Characteristics:

  • Stored directly in memory (on the stack, for tech folks).

  • Includes most predefined types like int, double, bool, char, and structs like DateTime.

  • When assigned, a copy of the value is made.

Use Cases:

  • Storing simple data like ages, prices, or counts.

  • Representing dates or times (e.g., DateTime).

  • Using in calculations where you don’t want the original value to change accidentally.

Code Sample :

using System;

class Program
{
    static void Main()
    {
        // Value type: int
        int original = 10;
        int copy = original; // Creates a copy of the value
        copy = 20; // Changing copy doesn't affect original

        Console.WriteLine($"Original: {original}"); // Still 10
        Console.WriteLine($"Copy: {copy}"); // Now 20

        // Value type: struct (DateTime)
        DateTime date = DateTime.Now;
        DateTime copyDate = date; // Creates a copy
        copyDate = copyDate.AddDays(1); // Changing copy doesn't affect original

        Console.WriteLine($"Original Date: {date}");
        Console.WriteLine($"Copy Date: {copyDate}");
    }

Example:

  • Scenario: A program tracks a person’s age and the current date.

  • Value Types: int for age, DateTime for the date.

  • Behavior: Changing the copy variable (age) or copyDate (date) doesn’t affect original or date.

  • Output (example, assuming today is May 28, 2025):

Output :

2. Reference Types

Description: A reference type is like a treasure map. Instead of holding the treasure (data) directly, it holds a pointer (or address) to where the treasure is stored. When you give someone the map (assign it to another variable), you’re giving them a copy of the map, not the treasure. Both maps point to the same treasure, so changing the treasure affects everyone with a map.

Key Characteristics:

  • Stored in memory (on the heap), with variables holding a reference (pointer) to the data.

  • Includes classes, arrays, strings, and objects like List<T>.

  • When assigned, only the reference is copied, not the data itself.

Use Cases:

  • Storing complex data like a customer’s full profile (name, address, etc.).

  • Managing lists or collections (e.g., a list of students).

  • Sharing data across multiple parts of a program where changes should be reflected everywhere.

Code Sample:

using System;
using System.Collections.Generic;

class Person
{
    public string Name { get; set; }
}

class Program
{
    static void Main()
    {
        // Reference type: class
        Person originalPerson = new Person { Name = "Alice" };
        Person referencePerson = originalPerson; // Copies the reference, not the object

        referencePerson.Name = "Bob"; // Changes the same object

        Console.WriteLine($"Original Person Name: {originalPerson.Name}"); // Bob
        Console.WriteLine($"Reference Person Name: {referencePerson.Name}"); // Bob

        // Reference type: List
        List<int> numbers = new List<int> { 1, 2, 3 };
        List<int> referenceNumbers = numbers; // Copies the reference
        referenceNumbers.Add(4); // Changes the same list

        Console.WriteLine("Numbers: " + string.Join(", ", numbers)); // 1, 2, 3, 4
        Console.WriteLine("Reference Numbers: " + string.Join(", ", referenceNumbers)); // 1, 2, 3, 4
    }
}

Example:

  • Scenario: A program tracks a person’s name and a list of scores.

  • Reference Types: Person (a class) and List<int> (a collection).

  • Behavior: Changing reference Person.Name or adding to referenceNumbers affects the original because they point to the same data.

  • Output :

3. Nullable Types

Description: A nullable type is like a box that can either hold a value (like a number) or be empty (null). Normally, value types (like int) must have a value, but nullable types let them be “nothing” if needed. It’s like saying, “I don’t know the answer yet” instead of being forced to pick a number.

Key Characteristics:

  • Applies to value types (e.g., int, double, bool) using the ? symbol (e.g., int?).

  • Can hold a value or null.

  • Useful when a value might not be available or applicable.

Use Cases:

  • Storing optional data, like a middle initial that someone might not have.

  • Handling database fields that can be null (e.g., a “date of graduation” that’s not set yet).

  • Representing missing or unknown values in calculations.

Code Sample

using System;

class Program
{
    static void Main()
    {
        // Nullable type: int?
        int? age = null; // No value yet
        Console.WriteLine($"Age: {(age.HasValue ? age.Value.ToString() : "Unknown")}");

        age = 30; // Now has a value
        Console.WriteLine($"Age: {age}");

        // Nullable type: double?
        double? temperature = null;
        Console.WriteLine($"Temperature: {(temperature.HasValue ? temperature.Value.ToString() : "Not recorded")}");

        temperature = 72.5;
        Console.WriteLine($"Temperature: {temperature}");
    }
}

Example:

  • Scenario: A form collects a person’s age and the room’s temperature, but the user might not provide either.

  • Nullable Types: int? for age, double? for temperature.

  • Behavior: The program checks if there’s a value using HasValue before using it.

  • Output :

8 views

C# Language And .Net

Part 1 of 5

This series contains all the core fundamentals and topics required to build a .net application. It will contain both theories and sample applications with automation testing.

Up next

Difference between dotnet Restore, Build & Run ( 01-D)

Understand what happens when you run the 3 most used dotnet cli commands.