Variables & basic types
var total = 5000m;Declares an inferred decimal variable for currency. Without m, C# defaults to double with precision issues.string city = "Dakar";Stores UTF-16 text. String is immutable, so modifications allocate a new object in memory.int? age = null;Nullable value type. Check HasValue before accessing Value to prevent an InvalidOperationException.const double TaxRate = 0.18;Compile-time constant value. It cannot be modified anywhere in your code after declaration.
Nullability & safety
string text = input ?? "Dakar";Null-coalescing operator: returns input if not null, else Dakar. Does not check for empty strings.int length = city?.Length ?? 0;Null-conditional chain: safely reads Length if city is not null, returning 0 as fallback.string name = user!.Name;Null-forgiving operator: suppresses null warning. Can cause NullReferenceException if actually null.if (input is string s)Pattern matching: checks type and assigns variable s if the value is non-null.
Control flow & modern syntax
string label = code switch { 221 => "SN", _ => "Other" };Concise switch expression. The underscore _ acts as the required fallback case.foreach (var item in list)Iterates over a collection. Modifying list inside the loop throws InvalidOperationException.record Merchant(string Name, string City);Creates an immutable value-based data type. Great for DTOs without writing boilerplate.using var stream = File.OpenRead(path);Disposes the resource automatically when the enclosing scope is exited.
LINQ - Filtering & projection
var active = users.Where(u => u.IsActive);Filters items with a predicate. LINQ uses deferred execution, so no query runs yet.var names = users.Select(u => u.Name);Projects elements into a new form. Does not execute until enumerated.var items = orders.SelectMany(o => o.Items);Flattens nested collections into a single flat sequence.var page = items.Skip(10).Take(5);Skips 10 items and takes the next 5. Commonly used for data pagination.
LINQ - Search & aggregation
var first = users.FirstOrDefault(u => u.Id == 1);Returns the first element or default (null) if none found. Safer than First().bool exists = users.Any(u => u.City == "Brazzaville");Checks if any element matches condition. Stops evaluating at the first match.decimal total = sales.Sum(s => s.Amount);Calculates sum of values. Throws ArgumentNullException if collection is null.var sorted = items.OrderBy(i => i.Price).ToList();Sorts items ascending and executes the query immediately via ToList().
LINQ - Grouping & conversions
var grouped = users.GroupBy(u => u.City);Groups elements by key. Each group holds a Key property and matching elements.var dict = users.ToDictionary(u => u.Id);Converts sequence to Dictionary. Throws ArgumentException if duplicate keys exist.var unique = items.DistinctBy(i => i.Code);Returns elements with unique Code values, keeping the first match encountered.var array = items.ToArray();Forces immediate query execution and materializes results into a native C# array.
Other resources
Need help with your website?
See the services I build your custom website.
Showcase site, online shop, management tool: let’s talk about your project.