我的C#快速参考

Syntax

  • Singed primitive types: sbyte, ushort, uint, ulong, built-in 128bit big number: decimal, plus every primitive Java has.
  • foreach keyword: for interate through IEnumerable, just like for in in Java
  • Data class equiv: record
  • Support vararg and default params
  • ref and out : pass an argument by reference. out does 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 struct Nullable<T>, still it’s more efficient than Java’s boxed objects.
  • HasValue and ?? : same as != null and ? in Kotlin
  • Lambda: Func<int, int> f = (x) => x * x
  • using: automatically clean IDisposable once executed out of the scope
  • Dynamic objects: using System.Dynamic.ExpandoObject to dynamically create fields.
  • IQueryable: query abilites for collection types: Sort, Select(map), Where(filter), reduce.
  • Type inference: var notice there is another keyword readonly to 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#’s super
  • Secondary constructors can “inherit” base or this constructors, just like Kotlin
  • Indexer of a class: public string this[int i] { get {..}; set {...}; }
  • override is a KW: unlike in Java, like Kotlin.
  • virtual : same as open in Kotlin, Java only uses final
  • partial: 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.