C# for Beginners: Essential Concepts to Get Started#
C# (C-Sharp) is a modern, object-oriented, and type-safe language developed by Microsoft. Along with the .NET platform, it is widely used to develop web applications (ASP.NET), desktop, mobile, and video games (via Unity).
Here is a practical cheatsheet to navigate the basic concepts and modern features of C#.
1. Basic Structure (Top-Level Statements)#
In modern C# (from 9 onwards), it is no longer necessary to write all the tedious boilerplate code (class Program, static void Main) for simple programs. The compiler generates it for you.
using System;
Console.WriteLine("Hello, World!");using System;= includes base types, such asConsole.Console.WriteLine= prints a line of text to the console and moves to a new line.
2. Variables, Types and Inference#
C# is strongly typed, but allows using var to let the compiler infer the type when it is obvious from the context.
int age = 25; // Integer
double price = 19.99; // 64-bit floating point
decimal balance = 100.50m; // Great for financial data (m stands for money/decimal)
bool isActive = true; // Boolean
string name = "Ashif"; // String
char initial = 'A'; // Single character
// Type inference: the compiler knows 'greeting' is a string
var greeting = "Good morning!";
// String interpolation (put the $ symbol before the quotes)
Console.WriteLine($"My name is {name} and I am {age} years old.");3. Input and Arrays / Lists#
List<T> is the equivalent of vector in C++ or lists in Python: dynamic arrays that can grow in size.
using System;
using System.Collections.Generic;
Console.Write("Enter your name: ");
string input = Console.ReadLine(); // Reads from the keyboard
// Dynamic list of strings
List<string> languages = new List<string> { "C#", "C++", "JavaScript" };
languages.Add("Python");
foreach (var lang in languages)
{
Console.WriteLine(lang);
}4. Control Structures and Pattern Matching#
Besides the classic if/else, C# offers a powerful switch which, in modern versions, supports advanced pattern matching.
int code = 404;
// Classic switch statement:
switch (code)
{
case 200:
Console.WriteLine("OK");
break;
case 404:
Console.WriteLine("Not Found");
break;
default:
Console.WriteLine("Unknown Error");
break;
}
// Switch Expression (more concise, C# 8+):
string message = code switch
{
200 => "Everything ok",
400 or 401 or 404 => "Client error", // Logical pattern matching (C# 9+)
>= 500 => "Server error", // Relational pattern matching (C# 9+)
_ => "Unhandled code" // Default (discard)
};5. Classes and OOP (Properties)#
C# makes object-oriented programming very elegant thanks to Properties, which replace the typical get/set methods of Java or C++.
public class Person
{
// Property: looks like a public variable, but hides getter and setter!
public string Name { get; set; }
// Auto-property init-only (can only be set upon creation)
public int Age { get; init; }
// Constructor
public Person(string name, int age)
{
Name = name;
Age = age;
}
public void Greet()
{
Console.WriteLine($"Hi, I'm {Name} and I'm {Age} years old.");
}
}
// Usage:
Person p = new Person("Ashif", 25);
// Or via Object Initializer (requires empty constructor or optional parameters):
Person p2 = new Person { Name = "Mario", Age = 30 };6. Records (C# 9+)#
Records are a special type geared towards immutable data. Ideal for DTOs (Data Transfer Objects), API responses, or models that shouldn’t change state.
// A record automatically generates constructor, init-only properties,
// and methods to compare values instead of references!
public record Product(string Name, decimal Price);
var p1 = new Product("Laptop", 1200.00m);
var p2 = new Product("Laptop", 1200.00m);
Console.WriteLine(p1 == p2); // True! In a normal class it would be False.
// Non-destructive mutation (creates a copy with a different value)
var p3 = p1 with { Price = 999.00m };7. LINQ (Language Integrated Query)#
LINQ is perhaps the most beloved feature of C#. It allows manipulating data collections using a declarative functional syntax (similar to SQL or the .map()/.filter() methods of JavaScript).
using System.Linq;
using System.Collections.Generic;
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Filter even numbers and multiply them by 10
var results = numbers
.Where(n => n % 2 == 0) // Lambda expression to filter
.Select(n => n * 10) // Data transformation
.ToList(); // Immediate execution and conversion to list
// results will contain: [20, 40, 60, 80, 100]8. Asynchronous Programming (async / await)#
In C# handling slow operations (like network calls or disk I/O) is extremely easy and does not block the main application thread.
using System.Net.Http;
using System.Threading.Tasks;
// The method is marked 'async' and returns a 'Task'
async Task<string> DownloadTextAsync(string url)
{
using HttpClient client = new HttpClient(); // 'using' automatically closes/disposes
// 'await' suspends execution until the download is complete,
// freeing up the thread in the meantime!
string content = await client.GetStringAsync(url);
return content;
}
// Usage (in top-level statements or another async method):
string outcome = await DownloadTextAsync("https://example.com");
Console.WriteLine($"Downloaded {outcome.Length} characters.");With these basics (Properties, LINQ, and asynchronous tasks) you are already ready to start developing serious applications, whether it’s a web backend with ASP.NET Core, or a game with Unity!




