C# Quick Start
Syntax
- Singed primitive types:
sbyte,ushort,uint,ulong, built-in 128bit big number:decimal, plus every primitive Java has. foreachkeyword: for interate throughIEnumerable, just likefor inin Java- Data class equiv:
record - Support vararg and default params
refandout: pass an argument by reference.outdoes not require it to be initialized first, so the method can initialize it and then after calling method the caller can access it, no return needed.yeild return: return enumerable;yeild break: dont include in the enumerable- Named params:
Method(2, 3, "some", other: 4) - Support extension methods
- Nullable variable through
?, however under the hood it’s a structNullable<T>, still it’s more efficient than Java’s boxed objects. HasValueand??: same as!= nulland?in Kotlin- Lambda:
Func<int, int> f = (x) => x * x using: automatically cleanIDisposableonce executed out of the scope- Dynamic objects: using
System.Dynamic.ExpandoObjectto dynamically create fields. IQueryable: query abilites for collection types:Sort,Select(map),Where(filter), reduce.- Type inference:
varnotice there is another keywordreadonlyto enforce immutablity - Implicit objects based on anonymous types:
bikes.Select(b=>new { Name = b.Name, IsAwesome = !b.IsBroken && b.HasTassles }); - Parallel LINQ:
bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name);The system will take care of all the threading and sync - LINQ execution is delayed: the operations only fire when data accessed
catch(...) when (..)- Static
using: Direct access static members without the class name - Tuples (C#7 and later): can have multiple elements, can be destructured, can have property names.
- Pattern Matching
- Extreme flexibility for if and switch expressions and many sugaring.
- Local Functions: Nest function in function
OOP
- Access modifiers:
public/private/protected/internal - get/set method sugaring:
public int i {get { ... }; private set;} - readonly properties that actcually return an expression of other properties:
public string LongName => Name + " " + _speed + " speed"; base: C#’ssuper- Secondary constructors can “inherit”
baseorthisconstructors, just like Kotlin - Indexer of a class:
public string this[int i] { get {..}; set {...}; } overrideis a KW: unlike in Java, like Kotlin.virtual: same asopenin Kotlin, Java only usesfinalpartial: you can write a class in multiple files.- Attribute: C#’s annotation
- Delegates and Events: Type-safe function pointers
- Delegate:
public delegate void MyDelegate(string message);to decl a delegate type, then can be initialized with a function that matches signature. - Multicast/Compose: can use
+operator to chain multiple function to a delegate. - Event: can be created using a delegate type:
public static event MyDelegate MyEvent;and can use + or - to modify it’s observers. used in event handling in GUI frameworks.
- Delegate:
This article uses CC BY-SA 4.0 License. You may need to give appropriate credit, provide a link to the license, and indicate if changes were made when referencing this article.