techmore.in

C# - Data Types

In C#, data types define the kind of data a variable can hold. They are categorized into two main types: value types and reference types. Understanding these types is crucial for effective programming and memory management.

1. Value Types

Value types store their data directly. When a value type variable is assigned to another variable, a copy of the value is made. Value types are usually stored on the stack.

Primitive Value Types:

  • bool: Represents a boolean value (true or false).

    csharp
    bool isTrue = true;
  • byte: Represents an 8-bit unsigned integer (0 to 255).

    csharp
    byte age = 25;
  • sbyte: Represents an 8-bit signed integer (-128 to 127).

    csharp
    sbyte temperature = -10;
  • short: Represents a 16-bit signed integer (-32,768 to 32,767).

    csharp
    short distance = 1200;
  • ushort: Represents a 16-bit unsigned integer (0 to 65,535).

    csharp
    ushort population = 50000;
  • int: Represents a 32-bit signed integer (-2,147,483,648 to 2,147,483,647).

    csharp
    int salary = 3000;
  • uint: Represents a 32-bit unsigned integer (0 to 4,294,967,295).

    csharp
    uint fileSize = 1048576;
  • long: Represents a 64-bit signed integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).

    csharp
    long distanceToMoon = 384400000;
  • ulong: Represents a 64-bit unsigned integer (0 to 18,446,744,073,709,551,615).

    csharp
    ulong universeAge = 13800000000;
  • float: Represents a 32-bit floating-point number (approximately ±1.5 x 10^−45 to ±3.4 x 10^38).

    csharp
    float pi = 3.14f;
  • double: Represents a 64-bit floating-point number (approximately ±5.0 x 10^−324 to ±1.7 x 10^308).

    csharp
    double gravity = 9.81;
  • decimal: Represents a 128-bit precise decimal number (±1.0 x 10^−28 to ±7.9 x 10^28) with 28-29 significant digits. Useful for financial calculations.

    csharp
    decimal price = 19.99m;
  • char: Represents a single 16-bit Unicode character.

    csharp
    char initial = 'A';

Structs

Structures (struct) are user-defined value types that can contain data members and methods.

csharp
struct Point { public int X; public int Y; public Point(int x, int y) { X = x; Y = y; } } Point p = new Point(10, 20);

2. Reference Types

Reference types store a reference to their data, which is typically allocated on the heap. When a reference type variable is assigned to another variable, both variables refer to the same object.

Built-In Reference Types:

  • string: Represents a sequence of characters. Immutable.

    csharp
    string greeting = "Hello, World!";
  • object: The base type of all other types. Can hold any data type.

    csharp
    object obj = 42; // Can hold different types obj = "Hello";

Classes

Classes are user-defined reference types that can contain data members, methods, and other members.

csharp
class Person { public string Name { get; set; } public int Age { get; set; } public void Introduce() { Console.WriteLine($"Hi, I'm {Name} and I'm {Age} years old."); } } Person person = new Person { Name = "Alice", Age = 30 };

Arrays

Arrays are collections of variables of the same type. They are reference types, but their elements are value types or reference types depending on their data type.

csharp
int[] numbers = new int[5]; numbers[0] = 1; numbers[1] = 2;

3. Nullable Types

Nullable types allow value types to represent null values. They are created using the ? syntax.

csharp
int? nullableInt = null; // Nullable int

4. Type Inference

C# allows type inference using the var keyword, where the compiler determines the type based on the assigned value.

csharp
var count = 10; // int var name = "Alice"; // string var price = 19.99; // double

Summary

C# provides a variety of data types to accommodate different kinds of data. Value types, such as int, float, and char, store data directly and are managed on the stack. Reference types, such as string, class, and object, store references to objects on the heap. Nullable types extend value types to represent null, and type inference with var simplifies variable declaration. Understanding these types is fundamental for effective C# programming and memory management.