After this lesson, you will be able to: Use C# fundamentals: types, control flow, methods, namespaces, using statements.
C# is a statically typed, OOP-first language with strong tooling. Coming from Java? Almost identical. From Python? Get used to types.
Statically typed. Type can be explicit or inferred.
// Explicit typesint count = 10;double price = 9.99;string name = "Alex";bool active = true;char grade = 'A';// Type inference with var (still statically typed!)var n = 42; // n is intvar s = "hello"; // s is stringvar list = new List<int>();// Nullable reference types (C# 8+, enabled by default in new projects)string nonNullable = "x"; // can't be nullstring? nullable = null; // can be null, the ? matters// Constantsconst double PI = 3.14159;readonly string AppName = "LastWrite"; // readonly = set once at init
If, switch, for, foreach, methods.
// if / elseint x = 10;if (x > 5) Console.WriteLine("big");else if (x == 0) Console.WriteLine("zero");else Console.WriteLine("small");// switch expression (modern C# 8+)string label = x switch {> 100 => "huge",> 10 => "big",0 => "zero",_ => "small",};// for + foreachfor (int i = 0; i < 5; i++) Console.WriteLine(i);var nums = new[] { 1, 2, 3 };foreach (var n in nums) Console.WriteLine(n);// Methodsint Square(int x) => x * x; // expression-bodiedstatic int Add(int a, int b) {return a + b;}// Optional + named argsstatic int Power(int base_, int exp = 2) => (int)Math.Pow(base_, exp);Power(3); // 9Power(3, exp: 3); // 27, named arg
How C# organizes code.
// namespace block (legacy)namespace MyApp.Users {public class User { }}// File-scoped namespace (C# 10+, preferred)namespace MyApp.Users;public class User { }// using directives at the top of a fileusing System; // optional in C# 10+ (auto-imported)using System.Collections.Generic;using System.Linq;// Global usings (C# 10+), declare once in any file, applies project-wideglobal using System.Threading.Tasks;
Mixing camelCase and PascalCase incorrectly (looks unprofessional in C# code). Using `var` for non-obvious types (`var x = GetData();`, what type?). Skipping the nullable annotation (`string` vs `string?`) when nullable enabled. Using `List<T>` when array would do; performance is similar but intent differs.
Sign in and purchase access to unlock this lesson.