Key Features of C#

Introduction to Key Features of C#

C# is a **powerful, modern, and object-oriented programming language** developed by Microsoft as part of the .NET framework. It combines the **ease of a high-level language** with features that make it suitable for enterprise, web, mobile, cloud, and game development. Let’s explore the core features that make C# one of the most preferred programming languages.

1. Strongly Typed Language

C# is a **strongly typed language**, meaning that **every variable and expression must have a defined type**. This ensures **type safety**, reducing errors and unexpected behavior during runtime.

Example: Type Safety in C#

using System;

class Program
{
    static void Main()
    {
        int number = 10;
        // number = "Hello"; // Error: Cannot assign string to an integer
        Console.WriteLine(number);
    }
}

        

The compiler **prevents assigning an incorrect type** (e.g., a string to an integer), ensuring data integrity.

2. Automatic Garbage Collection

C# uses **automatic garbage collection**, meaning that the **.NET runtime manages memory allocation and deallocation** automatically. This helps **prevent memory leaks** and reduces the need for manual memory management.

Example: Garbage Collection in Action

using System;

class Program
{
    static void Main()
    {
        for (int i = 0; i < 100000; i++)
        {
            var obj = new object(); // Allocating objects in memory
        }
        Console.WriteLine("Garbage Collection will free unused memory.");
    }
}

        

The **Garbage Collector (GC)** periodically reclaims memory occupied by objects **no longer in use**.

3. LINQ (Language-Integrated Query)

**LINQ** (Language-Integrated Query) enables developers to write **queries directly in C#** to retrieve and manipulate data from collections, databases, XML, and more.

Example: Querying a List Using LINQ

using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List numbers = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        // LINQ query to get even numbers
        var evenNumbers = numbers.Where(num => num % 2 == 0);

        Console.WriteLine("Even numbers: " + string.Join(", ", evenNumbers));
    }
}

        

4. Asynchronous Programming

C# has built-in support for **asynchronous programming** using **async and await**, making it easy to perform **non-blocking operations** like file I/O, database access, and network requests.

Example: Asynchronous Method Using `async` and `await`

using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        Console.WriteLine("Task starting...");
        await Task.Delay(3000);  // Simulates a delay
        Console.WriteLine("Task completed.");
    }
}

        

5. Interoperability

C# provides excellent **interoperability**, allowing it to work with other programming languages like **C, C++, and F#**. This makes C# ideal for **integrating with existing systems**.

6. Unified Type System

C# follows a **unified type system**, meaning **all types, including primitive data types, inherit from `System.Object`**. This enables:

  • Consistent **type handling** across all data types.
  • Flexibility in **type conversions**.
  • Improved **object-oriented programming** capabilities.

Example: Unified Type System in Action

using System;

class Program
{
    static void Main()
    {
        object obj = 42;  // An integer stored in an object type
        Console.WriteLine(obj.ToString());  // Works for any data type
    }
}