Microsoft Learn

C# Language Documentation

Welcome to the comprehensive documentation for the C# programming language. C# is a modern, object-oriented, and type-safe programming language developed by Microsoft. It is designed for the .NET platform and is widely used for developing a broad range of applications, from web services and desktop applications to mobile apps and games.

Getting Started with C#

This section guides you through setting up your development environment and writing your first C# program. We cover installation of the .NET SDK and Visual Studio, and provide a step-by-step walkthrough of creating a simple "Hello, World!" application.

Your First C# Program

using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
    }
}

C# Language Basics

Understand the fundamental building blocks of the C# language:

Data Types

C# is a statically typed language, meaning that the type of a variable must be known at compile time. It offers a rich set of built-in data types:

Type Description Example
int 32-bit signed integer int count = 10;
double 64-bit floating-point number double price = 19.99;
bool Boolean (true or false) bool isActive = true;
string Sequence of characters string name = "Alice";
char Single character char initial = 'A';

Variables

Variables are named storage locations that hold values of a specific type. They must be declared before use.

int age; // Declaration
age = 30; // Assignment

string message = "Welcome!"; // Declaration and initialization

Operators

C# supports various operators for performing operations:

  • Arithmetic: +, -, *, /, %
  • Comparison: ==, !=, >, <, >=, <=
  • Logical: &&, ||, !
  • Assignment: =, +=, -=, etc.

Control Flow

Control flow statements determine the order in which code is executed. Key control flow structures include:

  • Conditional statements: if, else if, else, switch
  • Loops: for, while, do-while, foreach
  • Branching statements: break, continue, return

Object-Oriented Programming (OOP) in C#

C# is a powerful object-oriented language. Key OOP concepts include:

  • Classes and Objects: Blueprints for creating objects.
  • Encapsulation: Bundling data (fields) and methods that operate on the data within a single unit (class).
  • Inheritance: Allowing a new class to inherit properties and behaviors from an existing class.
  • Polymorphism: The ability of an object to take on many forms.
  • Abstraction: Hiding complex implementation details and showing only essential features.

Advanced C# Topics

Explore more complex features of C# that enable building sophisticated applications:

Asynchronous Programming

C# provides robust support for asynchronous operations using async and await keywords, allowing applications to remain responsive during long-running tasks.

Language Integrated Query (LINQ)

LINQ allows you to write queries against collections of data in a structured and declarative way, similar to SQL.

LINQ Example

var numbers = new List<int> { 1, 5, 8, 12, 3 };
var evenNumbers = from num in numbers
                  where num % 2 == 0
                  select num;

foreach (var num in evenNumbers)
{
    Console.WriteLine(num); // Outputs: 8, 12
}

Generics

Generics allow you to create reusable components that can work with any type, providing type safety and eliminating the need for casting.

API Reference

Access detailed documentation for the .NET Base Class Library (BCL) and other namespaces, providing information on classes, methods, properties, and more.

You can find the full API reference here.