Skip to content

C# Basics

These four tutorials cover the parts of C# that every other feature builds on. Each one is a hands-on page with compiled samples from one invented online shop, OrdersPortal, a complete working program at the end, and a table of the real compiler errors beginners hit. Read them in order: each page assumes the ones before it. If you already know another object-oriented language, skim the first two and slow down where C# differs: value versus reference types, properties, switch without fall-through, and interfaces.

  • Data: built-in types, var, value versus reference types, operators, arrays, namespaces and using directives.
  • Behavior: if, switch, loops, methods and their parameter kinds, and exceptions.
  • Object-oriented programming: classes, properties, constructors, access modifiers, static members, inheritance, abstract classes, interfaces and polymorphism.

You need the .NET 10 SDK (or later) and a terminal. The samples were compiled with .NET SDK 10.0.115.

  1. Types, variables and operators: declare typed variables, use arithmetic, comparison, logical and null operators, store values in arrays, and organize code with namespaces and using directives. You build an order-total calculator.
  2. Control flow and methods: branch with if and switch, repeat with loops, write methods with default, named, params, out and ref parameters, and handle errors with try, catch and finally. You build a checkout that validates and prices order lines.
  3. Classes and objects: define classes with properties and methods, write and chain constructors, protect state with access modifiers, and share data with static members. You build a shopping cart.
  4. Inheritance and interfaces: derive classes with base and override, share an algorithm with abstract classes, define contracts with interfaces, and let polymorphism choose the method at run time. You build an order processor with pluggable payment methods and notifiers.

Every basics topic has its own section on one of the four pages.

Topic Section
Variables and data types Types, variables and operators: variables
Operators Types, variables and operators: operators
Arrays Types, variables and operators: arrays
Namespaces Types, variables and operators: namespaces
Using directives Types, variables and operators: using directives
Control flow (if, switch, loops) Control flow and methods: control flow
Methods and parameters Control flow and methods: methods
Exception handling Control flow and methods: exceptions
Classes and objects Classes and objects
Constructors Classes and objects: constructors
Access modifiers Classes and objects: access modifiers
Static members Classes and objects: static members
Inheritance Inheritance and interfaces: inheritance
Abstract classes Inheritance and interfaces: abstract classes
Interfaces Inheritance and interfaces: interfaces
Polymorphism Inheritance and interfaces: polymorphism