C# Data Types

C# is a statically typed language, which means that the data type of a variable is known at compile time. This overview covers the various data types in C#, categorized into value types, reference types, and pointer types.

1. Value Types

Value types hold data directly and are stored in the stack. They include:

- Integral Types: Used for integer values.
- `byte`: 8-bit unsigned integer. Range: 0 to 255.
- `sbyte`: 8-bit signed integer. Range: -128 to 127.
- `short`: 16-bit signed integer. Range: -32,768 to 32,767.
- `ushort`: 16-bit unsigned integer. Range: 0 to 65,535.
- `int`: 32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647.
- `uint`: 32-bit unsigned integer. Range: 0 to 4,294,967,295.
- `long`: 64-bit signed integer. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
- `ulong`: 64-bit unsigned integer. Range: 0 to 18,446,744,073,709,551,615.

- Floating Point Types: Used for decimal values.
- `float`: 32-bit single precision. Range: ±1.5 × 10^−45 to ±3.4 × 10^38. Precision: ~6-9 digits.
- `double`: 64-bit double precision. Range: ±5.0 × 10^−324 to ±1.7 × 10^308. Precision: ~15-17 digits.
- `decimal`: 128-bit precise decimal. Range: ±1.0 × 10^−28 to ±7.9 × 10^28. Precision: 28-29 significant digits. Useful for financial calculations.

- `char`: 16-bit Unicode character. Represents a single character. Range: U+0000 to U+FFFF.

- `bool`: Represents a boolean value. Can be either `true` or `false`.

Example:
using System;

class Program
{
    static void Main()
    {
        int age = 30;
        float height = 5.9f;
        bool isStudent = false;
        char grade = 'A';
        decimal price = 199.99m;

        Console.WriteLine($"Age: {age}, Height: {height}, Is Student: {isStudent}, Grade: {grade}, Price: {price}");
    }
}

Output:
Age: 30, Height: 5.9, Is Student: False, Grade: A, Price: 199.99

2. Reference Types

Reference types store references to the actual data, which is stored in the heap. They include:

- `string`: Represents a sequence of characters. Strings are immutable.

Example:
using System;

class Program
{
    static void Main()
    {
        string name = "John Doe";
        Console.WriteLine("Name: " + name);
    }
}

Output:
Name: John Doe


- Arrays: A collection of items of the same type, stored in a contiguous memory location.

Example:
using System;

class Program
{
    static void Main()
    {
        int[] numbers = { 1, 2, 3, 4, 5 };
        Console.WriteLine("Numbers: " + string.Join(", ", numbers));
    }
}

Output:
Numbers: 1, 2, 3, 4, 5


- Classes: A blueprint for creating objects. Classes can contain fields, methods, properties, and events.

Example:
using System;

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.");
    }
}

class Program
{
    static void Main()
    {
        Person person = new Person { Name = "Alice", Age = 25 };
        person.Introduce(); // Output: Hi, I'm Alice and I'm 25 years old.
    }
}

Output:
Hi, I'm Alice and I'm 25 years old.


- Interfaces: Define a contract for classes. Classes that implement an interface must provide implementations for its members.

Example:
using System;

interface IAnimal
{
    void Speak();
}

class Dog : IAnimal
{
    public void Speak()
    {
        Console.WriteLine("Woof!");
    }
}

class Program
{
    static void Main()
    {
        IAnimal animal = new Dog();
        animal.Speak(); // Output: Woof!
    }
}

Output:
Woof!


- Delegates: Type that represents references to methods with a specific parameter list and return type.

Example:

using System;

delegate void Greet(string name);

class Program
{
    static void Main()
    {
        Greet greet = Hello;
        greet("John"); // Output: Hello, John!
    }

    static void Hello(string name)
    {
        Console.WriteLine("Hello, " + name + "!");
    }
}

Output:
Hello, John!

3. Pointer Types

Pointer types are used to store the address of another type. They are mostly used in unsafe code contexts.

- `pointer`: A variable that holds the address of another variable. Pointer types can be declared using the `*` syntax.

Example:

using System;

class Program
{
    static unsafe void Main()
    {
        int number = 10;
        int* pNumber = &number;
        Console.WriteLine("Value: " + *pNumber); // Output: 10
    }
}

Output:
Value: 10

4. Nullable Types

Nullable types allow value types to be assigned a null value. This is useful when working with databases or optional values.

- Syntax: `type?` Example:
using System;

class Program
{
    static void Main()
    {
        int? age = null;
        Console.WriteLine("Age: " + (age.HasValue ? age.ToString() : "No age provided")); // Output: No age provided
    }
}

Output:
Age: No age provided

Summary
C# provides a rich set of data types to handle various kinds of data. Understanding these data types and their appropriate use is crucial for effective programming in C#. This detailed overview covers the primary categories of data types, along with examples demonstrating their functionality in real-world applications.

Previous: C# Internals | Next: C# Operators

<
>