Back to Curriculum

Variables and Data Types

📚 Lesson 2 of 15 ⏱️ 30 min

Variables and Data Types

30 min

C# is a strongly-typed language with value types and reference types, providing compile-time type checking that prevents many errors. Value types store data directly in memory (stack), while reference types store references to data on the heap. This distinction affects memory usage, performance, and behavior. Understanding types enables writing efficient, correct C# code. Type safety is a core C# feature.

Common value types include int (32-bit integer), double (64-bit floating-point), bool (boolean), char (16-bit Unicode character), and structs (user-defined value types). Value types are copied when assigned or passed as parameters, meaning each variable has its own copy. Value types are efficient for small data. Understanding value types enables appropriate type selection. Value types are ideal for simple data.

Reference types include classes, interfaces, arrays, and strings. Reference types store references (memory addresses) to data on the heap. Multiple variables can reference the same object, so changes affect all references. Reference types enable complex data structures and object-oriented programming. Understanding reference types enables effective OOP. Reference types are essential for complex applications.

C# supports type inference with the 'var' keyword, allowing the compiler to determine the type from the initializer. 'var' is useful for long type names (like Dictionary<string, List<int>>) and LINQ queries. However, 'var' doesn't make C# dynamically typed—the type is still determined at compile time. Understanding 'var' enables cleaner code. Use 'var' when the type is obvious from context.

C# provides nullable value types (e.g., int?) for value types that can be null, and nullable reference types (C# 8.0+) for reference types. Nullable types enable representing 'no value' scenarios. Nullable reference types help prevent NullReferenceException by making nullability explicit. Understanding nullable types enables safer code. Nullable types are essential for modern C#.

Best practices include using meaningful variable names, choosing appropriate types (int vs long, float vs double), using 'var' when the type is obvious, initializing variables before use, and understanding value vs reference semantics. Understanding variables and types enables writing clear, efficient C# code. Type system knowledge is fundamental to C# programming.

Key Concepts

  • C# is strongly-typed with value types and reference types.
  • Value types store data directly; reference types store references.
  • Common value types: int, double, bool, char, structs.
  • Reference types: classes, interfaces, arrays, strings.
  • C# supports type inference with 'var' keyword.

Learning Objectives

Master

  • Understanding value types vs reference types
  • Declaring variables with appropriate types
  • Using type inference with 'var'
  • Working with nullable types

Develop

  • Understanding memory management in C#
  • Choosing appropriate types for different scenarios
  • Appreciating C#'s type system

Tips

  • Use 'var' when the type is obvious from the initializer.
  • Understand that value types are copied, reference types share references.
  • Use nullable types (int?) when a value might not exist.
  • Choose appropriate numeric types (int vs long, float vs double).

Common Pitfalls

  • Confusing value types with reference types, causing unexpected behavior.
  • Not understanding that structs are value types, causing performance issues.
  • Using 'var' when the type isn't obvious, reducing code readability.
  • Not initializing variables, causing compilation errors.

Summary

  • C# is strongly-typed with value and reference types.
  • Value types store data directly; reference types store references.
  • Type inference with 'var' enables cleaner code.
  • Understanding types enables efficient, correct C# code.
  • Type system knowledge is fundamental to C# programming.

Exercise

Declare variables of different types and demonstrate type inference.

using System;

class Program
{
    static void Main(string[] args)
    {
        // Value types
        int age = 25;
        double height = 1.75;
        bool isStudent = true;
        char grade = 'A';
        
        // Reference types
        string name = "John Doe";
        var city = "New York"; // Type inference
        
        Console.WriteLine($"Name: {name}, Age: {age}");
        Console.WriteLine($"Height: {height}m, Student: {isStudent}");
        Console.WriteLine($"Grade: {grade}, City: {city}");
    }
}

Code Editor

Output