MSDN Documentation

Microsoft Developer Network

C# Language Features

Explore the powerful and versatile features of the C# programming language, designed for building modern, robust, and scalable applications on the .NET platform.

Introduction to C#

C# is a modern, object-oriented, and type-safe programming language developed by Microsoft. It is designed for building a wide range of applications, from web services and desktop applications to mobile apps and games.

Key Language Features

Example: Async/Await

The async and await keywords make it easier to write asynchronous code. Consider this example for downloading web content:


using System;
using System.Net.Http;
using System.Threading.Tasks;

public class AsyncExample
{
    public static async Task DownloadContentAsync(string url)
    {
        using (HttpClient client = new HttpClient())
        {
            try
            {
                Console.WriteLine($"Downloading from: {url}");
                string content = await client.GetStringAsync(url);
                Console.WriteLine("Download complete.");
                // Process the content here
                // Console.WriteLine($"Content length: {content.Length}");
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Error downloading content: {e.Message}");
            }
        }
    }

    public static async Task Main(string[] args)
    {
        string sampleUrl = "https://www.example.com";
        await DownloadContentAsync(sampleUrl);
        Console.WriteLine("Main method finished.");
    }
}
            

Example: LINQ

LINQ simplifies data manipulation. Here's an example of filtering and sorting a list of numbers:


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

public class LinqExample
{
    public static void Main(string[] args)
    {
        List numbers = new List { 5, 2, 8, 1, 9, 4, 7, 3, 6 };

        // Filter for even numbers greater than 3, then sort them
        var filteredAndSorted = numbers
            .Where(n => n % 2 == 0 && n > 3)
            .OrderBy(n => n)
            .ToList();

        Console.WriteLine("Filtered and sorted even numbers > 3:");
        foreach (var number in filteredAndSorted)
        {
            Console.WriteLine(number);
        }
    }
}
            

Further Reading