Key Features of C#

Strongly Typed

Every variable and expression in C# must have a type, ensuring type-safe operations. This helps in avoiding unexpected errors during runtime by ensuring that only valid operations are performed on variables.

Automatic Garbage Collection

Memory management is handled automatically in C# through garbage collection. The .NET runtime monitors and frees up memory that is no longer in use, helping developers avoid common issues like memory leaks and freeing them from manual memory management.

LINQ (Language-Integrated Query)

LINQ enables developers to write queries directly in C# to retrieve data from various data sources, such as databases, collections, or XML documents. This integration of query capabilities into the language itself allows for a more declarative approach to working with data.

Example LINQ Query in C#:

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

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

        // LINQ query to get even numbers
        var evenNumbers = from num in numbers
                          where num % 2 == 0
                          select num;

        // Display results
        Console.WriteLine("Even numbers:");
        foreach (var num in evenNumbers)
        {
            Console.WriteLine(num);
        }
    }
}
        
    

Asynchronous Programming

Built-in support for asynchronous programming in C# allows applications to perform tasks concurrently without blocking the main thread. This is especially useful for tasks like file I/O or web requests that may take a significant amount of time to complete.

Example of Asynchronous Programming in C#:

    
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // Calling an async method
        await PerformTaskAsync();
    }

    static async Task PerformTaskAsync()
    {
        Console.WriteLine("Task starting...");
        await Task.Delay(3000);  // Simulate a 3-second task
        Console.WriteLine("Task completed.");
    }
}
        
    

Interoperability

C# offers interoperability, allowing it to interact with code written in other languages such as C++ or F#. This makes C# a flexible language that can be integrated into existing projects or systems where multiple languages are in use.

Unified Type System

In C#, all types are derived from a single base class called object. This unified type system allows for flexibility and consistency in the way types are handled in the language. It also simplifies type conversions and interactions between different types of data.