C# Beginner
Multi-paradigm language — enterprise-grade OOP, cross-platform performance via .NET, native garbage collection, and powerful type-safe queries.
Table of Contents
Entry Point & Variables
Modern C# implements **Top-Level Statements**, removing boilerplate code like namespaces and class declarations for entry files. Variables can use explicit types or infer them via the var keyword while maintaining strict type safety.
// Single-line comment /* Multi-line comment block */ using System; string project = "testProject"; var operationalState = "Active"; // Implicitly typed as string // String interpolation using the $ prefix Console.WriteLine($"System core: {project} is currently {operationalState}.");
Data Types & Generics
C# distinguishes between Value Types (stored on the stack) and Reference Types (stored on the heap). Collections like List<T> enforce type safety through generics.
Conditionals & Logic Operations
Control application flow using standard if, else if, and else evaluation statements alongside explicit relational tracking operators.
int statusCode = 201; if (statusCode == 200 || statusCode == 201) { Console.WriteLine("Request processed successfully."); } else if (statusCode >= 400 && statusCode < 500) { Console.WriteLine("Client execution error handled."); } else { Console.WriteLine("Server or network fallback triggered."); }
Loops & Advanced Switch Patterns
Iterate over lists securely using foreach blocks. Modern C# also introduces powerful **Switch Expressions** that parse value configurations cleanly.
var platforms = new[] { "Web", "Android", "iOS" }; foreach (var platform in platforms) { // C# 8+ Switch expression pattern matching assignment string accessLayer = platform switch { "Web" => "Browser runtime execution", "Android" => "Native APK bytecode environment", _ => "Generic architectural layer" // Discard fallback }; Console.WriteLine($"{platform}: {accessLayer}"); }
Methods & Auto-Properties
Methods declare parameters with strict visibility and return signatures. Use shorthand expressions (=>) for simple operations, and leverage **Auto-Implemented Properties** to safely encapsulate state fields.
public class AccountMetric { // Auto-property with restricted write access modifier public string Currency { get; private set; } = "EUR"; // Expression-bodied method mapping signature public bool IsValidTransaction(decimal value) => value > 0.00m; }
OOP Architecture & Records
C# implements a classic Object-Oriented paradigm featuring single class inheritance and explicit interface contracts. It also supports **Records**, which provide lightweight, immutable data structures out of the box.
public interface IRepository { void SaveState(); } // Class inheritance and explicit interface contract realization public class DbContext : IRepository { public void SaveState() => Console.WriteLine("Database persistence executed."); } // Immutable positional record model definition (C# 9+) public record UserProfile(string Username, string Email);
== evaluates to true.
Exceptions & Safe Resource Management
Handle runtime faults gracefully using try-catch-finally blocks. For external resources like network links or file streams, always use the using declaration statement to guarantee memory cleanup through `IDisposable` execution blocks.
try { // Modern scope-bound using statement layout context using var stream = new System.IO.StreamWriter("log.txt"); stream.WriteLine("Appending structural metadata block."); } catch (System.IO.IOException ex) { Console.WriteLine($"Disk storage mutation fault recorded: {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"Uncaught base execution error: {ex.Message}"); }
Data Querying via LINQ Expressions
**Language Integrated Query (LINQ)** provides a powerful, type-safe syntax to filter, transform, and aggregate data collections cleanly—whether operating on arrays or remote database entities.
var transactions = new List<decimal> { 120.50m, -45.00m, 300.00m, -12.30m }; // Clean method syntax query pipeline filtering values var highProfits = transactions .Where(tx => tx > 100.00m) .OrderByDescending(tx => tx); foreach (var profit in highProfits) { Console.WriteLine($"Filtered profit target node value: {profit}"); }