Skip to content
Justice Tech School

C# Exercises

Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.

C# Introduction

Exercise

Print a line.

Console.("Hi");

C# Get Started

Exercise

Run the project.

dotnet 

C# Syntax

Exercise

End a statement with this symbol.

Console.WriteLine("Hi")

C# Comments

Exercise

Start a single line comment.

 comment

C# Variables

Exercise

Declare an integer.

 n = 5;

C# Constants

Exercise

Declare a constant.

 int Max = 10;

C# Data Types

Exercise

Which type is best for money?

 price = 9.99m;

C# Type Casting

Exercise

Convert text to int safely.

int.("12", out int n)

C# User Input

Exercise

Read a line.

Console.();

C# Output and Formatting

Exercise

Insert a value into a string.

$"Hi {}"

Top Level Statements

Exercise

Print a line.

Console.("Hi");

var, const and readonly

Exercise

Constant keyword.

 int Max = 5;

Value Types

Exercise

Money type.

 price = 9.99m;

Nullable Types

Exercise

Default value.

n  0

Type Conversion

Exercise

Safe parse.

int.("5", out int n)

Console Input and Output

Exercise

Read a line.

Console.()

The Math Class

Exercise

Square root.

Math.(16)

Random Numbers

Exercise

Dice roll.

r.Next(1, )

C# Arithmetic Operators

Exercise

Remainder operator.

17  5

C# Assignment Operators

Exercise

Add 5 to x.

x  5;

C# Comparison and Logical Operators

Exercise

And operator.

a  b

C# Null Operators

Exercise

Default when null.

name  "unknown"

Arithmetic Operators

Exercise

Remainder.

7  2

Comparison and Logical Operators

Exercise

Logical and.

a  b

Null Operators

Exercise

Assign if null.

a  "x"

Ternary and Switch Expressions

Exercise

Default arm.

_  "many"

Bitwise Operators

Exercise

Shift left.

1  3

C# Strings

Exercise

Get the length.

s.

C# StringBuilder and Escapes

Exercise

Build a string efficiently.

new ()

String Basics

Exercise

Length.

s.

String Interpolation and Formatting

Exercise

Interpolate.

"Hi {name}"

More String Methods

Exercise

Split text.

s.(',')

StringBuilder

Exercise

Add text.

sb.("x")

Comparing Strings

Exercise

Ignore case option.

StringComparison.IgnoreCase

Verbatim and Raw Strings

Exercise

Verbatim prefix.

"C:\temp"

C# If and Else

Exercise

Second branch keyword.

if (a) {}  if (b) {}

C# Switch

Exercise

The fallback case.

switch (x) { : break; }

C# While and Do While

Exercise

Run at least once.

 { } while (x);

C# For Loops

Exercise

Count to 3.

for (int i = 0; i  3; i++)

C# Foreach

Exercise

Visit every item.

 (var x in list)

if, else if and else

Exercise

Else branch.

if (x) {}  {}

switch Statements

Exercise

End a case.

case 1: f(); ;

Pattern Matching

Exercise

Type test.

o  int n

for and while Loops

Exercise

Run once at least.

 { } while (c);

foreach

Exercise

Visit each item.

 (var x in list)

break, continue and goto

Exercise

Skip a round.

;

Ranges and Indices

Exercise

Last item.

a[1]

C# Arrays

Exercise

First index.

nums[]

C# Lists

Exercise

Add an item.

list.("x")

C# Dictionaries

Exercise

Safe lookup method.

dict.(key, out v)

C# LINQ Basics

Exercise

Keep matching items.

n.(x => x > 2)

Arrays

Exercise

Sort an array.

Array.(a)

List<T>

Exercise

Add an item.

l.("x")

Dictionary

Exercise

Safe read.

d.("k", out var v)

HashSet and Queue and Stack

Exercise

Take from a queue.

q.()

LINQ Basics

Exercise

Filter.

n.(x => x > 2)

LINQ GroupBy, Join and Aggregates

Exercise

Group.

words.(w => w[0])

Tuples and Deconstruction

Exercise

Group values.

var t = (1, );

Collection Initialisers and Spread

Exercise

Spread items.

[a, 4]

Custom Sorting

Exercise

Second sort key.

.OrderBy(a).(b)

C# Methods

Exercise

Return nothing.

static  Hi() {}

C# Parameters

Exercise

Accept many arguments.

static int F( int[] n)

C# Recursion and Lambdas

Exercise

Lambda arrow.

x  x * 2

Methods

Exercise

Return type for nothing.

static  Hi() {}

Optional and Named Arguments

Exercise

Named argument.

Box(w: 1, : 2)

params, ref and out

Exercise

Any number of args.

static int F( int[] n)

Method Overloading

Exercise

Overloads differ by.

Different 

Local Functions and Lambdas

Exercise

Lambda arrow.

x  x * 2

Recursion

Exercise

Base case.

if (n <= ) return 1;

C# Classes and Objects

Exercise

Create an object.

var s =  Student();

C# Constructors

Exercise

Constructor name matches the ___.

class Car { public () {} }

C# Properties

Exercise

Read accessor keyword.

public int X { ; set; }

C# Inheritance

Exercise

Symbol for inheritance.

class Dog  Animal

C# Interfaces and Abstract Classes

Exercise

Interface name starts with.

interface Shape

C# Static Members

Exercise

Belongs to the class.

public  int Count;

C# Enums, Structs and Records

Exercise

Short data type.

 Person(string Name);

Classes and Objects

Exercise

Create object.

var d =  Dog("Rex");

Properties

Exercise

Set once.

{ get; ; }

Access Modifiers

Exercise

Hide a field.

 decimal _b;

Inheritance

Exercise

Parent method call.

.Speak()

Interfaces

Exercise

Implement an interface.

class A :  {}

abstract and sealed

Exercise

Cannot instantiate.

 class A {}

static Members

Exercise

Type level member.

public  int n;

Records

Exercise

Copy with a change.

a  { Y = 5 }

Enums

Exercise

Convert to number.

()Day.Wed

Generics

Exercise

Constraint keyword.

 T : IComparable<T>

Extension Methods

Exercise

Extend a type.

Shout( string s)

ToString, Equals and GetHashCode

Exercise

Text of an object.

public override string () => "x";

C# Exceptions

Exercise

Always runs.

try {} catch {}  {}

C# Files

Exercise

Read all text.

File.(path)

try, catch and finally

Exercise

Always runs.

try {}  {}

throw and Custom Exceptions

Exercise

Raise.

 new Exception("x");

using and IDisposable

Exercise

Auto dispose.

 var r = new R();

Files with File and Path

Exercise

Combine paths.

Path.("a", "b")

JSON with System.Text.Json

Exercise

To JSON.

JsonSerializer.(obj)

Dates and Times

Exercise

Add days.

d.(10)

async and await

Exercise

Wait for a task.

 GetAsync()

Nullable Reference Types

Exercise

May be null.

string s

Practice: Word Frequency

Exercise

Count a group.

g.()

Practice: FizzBuzz

Exercise

Tuple pattern.

(a, b)  { }

Practice: Bank Account

Exercise

Private setter.

{ get;  set; }

C# Generics

Premium exercise. See plans

C# Delegates and Events

Premium exercise. See plans

C# Async and Await

Premium exercise. See plans

C# Advanced LINQ

Premium exercise. See plans

C# Pattern Matching

Premium exercise. See plans

C# Nullable Reference Types

Premium exercise. See plans

C# Dependency Injection

Premium exercise. See plans

ASP.NET Core Minimal API

Premium exercise. See plans

Entity Framework Core

Premium exercise. See plans

Unit Testing with xUnit

Premium exercise. See plans

Clean Code and Design Patterns

Premium exercise. See plans

Memory and Performance

Premium exercise. See plans

Packages, Projects and Publishing

Premium exercise. See plans