C# Data Types and Variables

    Atul Kabra3 min readUpdated

    In C#, every variable has a data type that tells the compiler what kind of value it holds and how much memory it needs. C# is statically typed, so the type is fixed when you declare the variable. The most common types are int (whole numbers), double (decimals), string (text), and bool (true/false). Types fall into two families: value types (the data lives directly in the variable) and reference types (the variable holds a reference to data on the heap). Understanding this split is key to writing correct C#.

    Common built-in types

    int count = 42;             // 32-bit whole number
    long bigNumber = 9_000_000; // 64-bit whole number (underscores are just for readability)
    double price = 199.99;      // 64-bit decimal — good for general maths
    decimal money = 199.99m;    // exact decimal — use for currency; note the 'm' suffix
    bool isOpen = true;         // true or false
    char grade = 'A';           // a single character in single quotes
    string name = "Infoplanet"; // text in double quotes
    

    Use decimal for money to avoid the tiny rounding errors that double can introduce.

    Value types vs reference types

    This is the single most important type concept in C#.

    // int is a VALUE type: each variable has its own copy.
    int x = 5;
    int y = x;   // y gets a COPY of 5
    y = 99;
    Console.WriteLine(x); // 5  — x is unaffected
    
    // Arrays (and classes) are REFERENCE types: variables can point to the same data.
    int[] a = { 1, 2, 3 };
    int[] b = a;   // b points to the SAME array as a
    b[0] = 99;
    Console.WriteLine(a[0]); // 99 — changing b changed a
    

    Value types include int, double, bool, char, decimal, and struct. Reference types include string, arrays, and any class. (string behaves like a value in practice because it is immutable.)

    Nullable types and null safety

    A value type normally cannot be null. Adding ? makes it nullable:

    int? maybeAge = null;   // allowed because of the ?
    maybeAge = 20;
    
    string? note = null;    // a nullable reference — may or may not hold text
    
    // Always check before using a value that could be null.
    if (maybeAge is not null)
    {
        Console.WriteLine(maybeAge.Value);
    }
    

    Modern .NET enables nullable reference types, which warn you at compile time when you might use a null. This prevents many crashes.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Safe type conversion

    Converting text to a number can fail. int.Parse throws an exception on bad input; int.TryParse is safer:

    string input = "123";
    
    // Parse: throws if the text is not a valid number.
    int n1 = int.Parse(input);          // 123
    
    // TryParse: returns true/false and never throws — the preferred way.
    if (int.TryParse(input, out int n2))
    {
        Console.WriteLine($"Parsed: {n2}"); // Parsed: 123
    }
    else
    {
        Console.WriteLine("Not a number");
    }
    

    For converting between numeric types you can cast: double d = 9.7; int i = (int)d; gives 9 (the decimal part is dropped, not rounded).

    Constants

    Use const for values that never change:

    const double Pi = 3.14159;  // cannot be reassigned anywhere
    const int MaxSeats = 30;
    

    Common mistakes

    • Using double for money. Floating-point types can produce 0.30000000000000004. Use decimal for currency.
    • Expecting (int)9.9 to round. Casting truncates to 9. Use Math.Round if you want rounding.
    • Forgetting that arrays and objects are shared. Assigning one variable to another copies the reference, not the data.
    • Calling int.Parse on user input directly. If the text isn't numeric it crashes. Prefer int.TryParse.
    • Ignoring nullable warnings. They exist to stop NullReferenceException before it happens.

    FAQ

    What's the difference between var and a specific type? var lets the compiler infer the type, but the type is still fixed. var x = 5; is exactly int x = 5;.

    Is string a value or reference type? Technically a reference type, but because strings are immutable they feel like value types day to day.

    Keep learning

    Want structured practice in Jalgaon? Join the waitlist for the Infoplanet .NET course at /courses/dotnet.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides