█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/C# and .NET/C# Fundamentals
50 minBeginner

C# Fundamentals

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.

Prerequisites:.NET Ecosystem

Variables and types

Statically typed. Type can be explicit or inferred.

tsx
// Explicit types
int 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 int
var s = "hello"; // s is string
var list = new List<int>();
// Nullable reference types (C# 8+, enabled by default in new projects)
string nonNullable = "x"; // can't be null
string? nullable = null; // can be null, the ? matters
// Constants
const double PI = 3.14159;
readonly string AppName = "LastWrite"; // readonly = set once at init

Control flow + methods

If, switch, for, foreach, methods.

tsx
// if / else
int 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 + foreach
for (int i = 0; i < 5; i++) Console.WriteLine(i);
var nums = new[] { 1, 2, 3 };
foreach (var n in nums) Console.WriteLine(n);
// Methods
int Square(int x) => x * x; // expression-bodied
static int Add(int a, int b) {
return a + b;
}
// Optional + named args
static int Power(int base_, int exp = 2) => (int)Math.Pow(base_, exp);
Power(3); // 9
Power(3, exp: 3); // 27, named arg

Namespaces + using

How C# organizes code.

tsx
// 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 file
using 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-wide
global using System.Threading.Tasks;

💡 Style conventions

PascalCase: classes, methods, properties, constants. (User, GetName, MaxRetries) camelCase: local variables, parameters. (userName, retryCount) Prefix private fields with `_`: (private int _count) Interfaces start with I: (IDisposable, IEnumerable<T>) Microsoft's official C# coding conventions: search 'csharp coding conventions'.

Common mistakes

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.

Sign in to purchase
←.NET Ecosystem and Modern C#
Back to C# and .NET
OOP in C#→