C#
Modern, statically typed, runs on .NET. Powers games, web, enterprise.
⚠ pattern-based checks (code is analyzed, not compiled)
About C#
C# is a modern, object-oriented language by Microsoft, running on .NET. It's used for Unity games, ASP.NET web apps, desktop apps, and more. NOTE: Code is checked by pattern matching, not compiled. Focus on idiomatic C#.
Quick-reference cheat sheet
// C# cheat-sheet
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
int x = 42;
string name = "Ada";
bool ok = true;
Console.WriteLine($"Hello, {name}!");
if (x > 0) Console.WriteLine("positive");
for (int i = 0; i < 5; i++) Console.WriteLine(i);
var nums = new List<int> { 1, 2, 3 };
nums.Add(4);
string line = Console.ReadLine();
int n = int.Parse(line);
}
static int Add(int a, int b) => a + b;
}
// Class with properties
class Person {
public string Name { get; set; }
public int Age { get; set; }
}
// LINQ
var evens = nums.Where(n => n % 2 == 0).ToList();
var sum = nums.Sum();
// Records (C# 9+)
public record Point(int X, int Y);
Tasks
- 01introHello, World!Console.WriteLine the greeting.
- 02introTyped variablesDeclare an int, a string, and a bool.
- 03introvar keywordUse var for type inference.
- 04easyif / elsePrint whether a number is positive, zero, or negative.
- 05easyswitch expressionUse the modern switch expression.
- 06easyFor loopPrint 1..10 with a for loop.
- 07easyforeach over arrayIterate an int[] with foreach.
- 08easyDefine a methodDefine static int Add(int a, int b).
- 09easyString interpolationInterpolate two values.
- 10mediumList<int>Create a List<int>, add 3 numbers, print the count.
- 11mediumDictionary<string,int>Build a string→int dictionary.
- 12mediumLINQ WhereUse LINQ Where to filter even numbers.
- 13mediumLINQ SelectUse Select to project squares.
- 14mediumLINQ Sum / AverageAggregate a sequence.
- 15mediumDefine a Person classClass Person with Name and Age properties.
- 16mediumConstructorAdd a constructor to a class.
- 17hardInheritanceSubclass Animal with Dog.
- 18hardInterfaceDefine interface IShape with double Area().
- 19hardAbstract classAbstract Shape class with abstract Area().
- 20hardGeneric methodDefine static T First<T>(T[] arr).
- 21mediumNullable typesUse int? and the ?? operator.
- 22mediumtry / catchCatch a FormatException.
- 23mediumLambda expressionAssign a Func<int,int,int>.
- 24hardRecords (C# 9+)Define record Point(int X, int Y).
- 25mediumTuple returnReturn a tuple from a method.
- 26mediumparams keywordVariadic method with params int[].
- 27hardDelegateDefine and use a delegate type.
- 28hardasync / awaitAsync method that awaits Task.Delay.
- 29hardLINQ GroupByGroup words by their length.
- 30hardExtension methodExtend string with .Shout().
- 31introRead & echo a nameConsole.ReadLine then greet.
- 32introParse and doubleint.Parse a line, multiply by 2.
- 33easyCircle areaUse Math.PI to compute πr².
- 34easyMin of three (ternary)Use Math.Min twice.
- 35easySwitch expressionConvert day number to name.
- 36easyForeach over arraySum an int[] with foreach.
- 37mediumStringBuilder loopBuild a string in a loop with StringBuilder.
- 38mediumReverse an arrayIn-place reverse via Array.Reverse.
- 39mediumFizzBuzzClassic 1..15 FizzBuzz with %.
- 40mediumIsPrime methodStatic bool IsPrime(int n).
- 41mediumList<T> basicsAdd items, then access by index.
- 42mediumDictionary word countCount word frequencies.
- 43mediumRecursive factoriallong Factorial(int n) recursive.
- 44mediumout parameterMethod returning two values via out.
- 45mediumtry / catch parseCatch FormatException.
- 46mediumClass with auto-propertiesDefine Student with Name & Grade.
- 47hardConstructor + ToStringOverride ToString in a class.
- 48hardInheritance & virtualAnimal -> Dog with override.
- 49hardImplement IComparable<T>Sortable Box by Volume.
- 50hardGeneric Swap<T>Generic method using ref.
- 51hardLINQ pipelineWhere + OrderByDescending + Select.
- 52hardTuple return + deconstructReturn (int min, int max).
- 53hardPattern matchingis-pattern with property check.
- 54hardRecord value equalityDefine a positional record.
- 55hardNull-coalescing & ?.Use ?. and ?? together.
- 56hardFile.WriteAllTextWrite then read a file.
- 57hardFunc<int,int,int>Lambda assigned to Func.
- 58easyVector2: a value typeDefine a readonly struct Vector2 with float X and Y.
- 59easyVector2: operator overloadingOverload operator + so two Vector2 values can be added.
- 60easyClamp player healthUse Math.Clamp(hp, 0, 100).
- 61easyDistance between two pointsCompute Math.Sqrt(dx*dx + dy*dy).
- 62mediumRandom spawn pointUse Random.Shared.Next(0, 100).
- 63mediumDirection enumDefine enum Direction { Up, Down, Left, Right }.
- 64mediumGame state switch expressionMap GameState to a label with a switch expression.
- 65mediumLinear interpolation (Lerp)Implement static float Lerp(float a, float b, float t).
- 66mediumFrame timing with StopwatchMeasure elapsed milliseconds with Stopwatch.
- 67mediumPlayer class with TakeDamageclass Player { int HP } with a TakeDamage(int) method.
- 68mediumIWeapon interfaceinterface IWeapon { int Damage; void Attack(); }
- 69mediumPosition as a record structrecord struct Position(int X, int Y);
- 70mediumDetect the host OSUse RuntimeInformation.IsOSPlatform.
- 71hardScore event with Action<int>Publish an event when score changes.
- 72hardAsync asset loaderasync Task<string> LoadAssetAsync(string path).
- 73hardType pattern matching on entitiesSwitch on the runtime type of an Entity.
- 74hardLINQ: closest 3 enemiesFilter enemies within range, order by distance, take 3.
- 75hardAABB collision checkAxis-aligned bounding box intersection test.
- 76hardTiny event busPub/sub on string channels with Action<object>.
- 77hardGeneric object poolPool<T> with Get and Return — avoids GC churn.
- 78hardCapstone: a tiny game loopPlayer + Enemy + Update(deltaTime) + score + game over.
