C# Programming Language

Table of Contents

Introduction

C# is a modern, object-oriented programming language developed by Microsoft as part of the .NET platform. It combines the power and flexibility of C++ with the simplicity of Visual Basic, making it an ideal choice for building a wide range of applications.

Key Characteristics:

Key Features

Language Features


// Properties
public class Person
{
    public string Name { get; set; }
    public int Age { get; private set; }

    private string _email;
    public string Email
    {
        get => _email;
        set => _email = value?.ToLower();
    }
}

// LINQ (Language Integrated Query)
var result = numbers
    .Where(n => n > 0)
    .Select(n => n * 2)
    .ToList();

// Async/Await
public async Task<string> GetDataAsync()
{
    using var client = new HttpClient();
    return await client.GetStringAsync("https://api.example.com/data");
}
    

Basic Syntax

Program Structure:


using System;

namespace MyApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            
            // Variables and Data Types
            int number = 42;
            string text = "Hello";
            bool flag = true;
            double pi = 3.14159;
            
            // Control Structures
            if (number > 0)
            {
                Console.WriteLine("Positive");
            }
            
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine($"Count: {i}");
            }
        }
    }
}
        

Object-Oriented Programming


// Class Definition
public class Animal
{
    protected string Name { get; set; }
    
    public Animal(string name)
    {
        Name = name;
    }
    
    public virtual void MakeSound()
    {
        Console.WriteLine("Some sound");
    }
}

// Inheritance
public class Dog : Animal
{
    public Dog(string name) : base(name)
    {
    }
    
    public override void MakeSound()
    {
        Console.WriteLine("Woof!");
    }
}

// Interface
public interface IMovable
{
    void Move();
}

// Implementation
public class Car : IMovable
{
    public void Move()
    {
        Console.WriteLine("Car is moving");
    }
}
    

Advanced Features

Modern C# Features:


// Pattern Matching
if (obj is string str)
{
    Console.WriteLine(str.Length);
}

// Records
public record Person(string Name, int Age);

// Switch Expressions
string GetSound(Animal animal) => animal switch
{
    Dog => "Woof",
    Cat => "Meow",
    _ => "Unknown"
};

// Nullable Reference Types
string? nullableString = null;
string nonNullString = "Hello";

// Top-level statements
using System;
Console.WriteLine("No class needed!");
        

.NET Ecosystem

Common Frameworks and Libraries:

Development Tools

Best Practices

Coding Guidelines:

Example of Clean Code:


public class OrderProcessor
{
    private readonly IRepository<Order> _orderRepository;
    private readonly IEmailService _emailService;

    public OrderProcessor(
        IRepository<Order> orderRepository,
        IEmailService emailService)
    {
        _orderRepository = orderRepository;
        _emailService = emailService;
    }

    public async Task ProcessOrderAsync(Order order)
    {
        try
        {
            await ValidateOrderAsync(order);
            await _orderRepository.SaveAsync(order);
            await _emailService.SendOrderConfirmationAsync(order);
        }
        catch (ValidationException ex)
        {
            // Handle validation errors
            throw;
        }
    }
}
    

Related Resources