It’s based on C# 7.0, which is the latest version with VS 2017. I list the feature unfamiliar to Java programmer.
Question | Answer |
---|---|
01.Losing precision:convert int to float and again back to int | floating-point has less precision than integer. Double has NO such issue. |
02.Rounding errors from float and double | Both are in base 2, so can lose precision for base 10 figures. decimal is base 10, but 10 times slower due to the lack of native support of CPU. |
03.Matrix (vs jagged arrays) | (see code) At least one int[,] can be omitted, but not both. Jagged arrays have the syntax like int[][]. |
04.ref and out parameter | out differs from ref modifier in that out arguments can be declared when being used in method invocation. |
05.Named arguments and optional parameters | For methods has several optional parameters, use named arguments for the sake of clarity. |
06.Null operators ? and ?? | ? turns an expression’s result to Option , while ?? does one more step further to call orElseGet method with laziness enabled. |
07.switch with patterns | switch supports types and null (C# 7, see code) |
08.Renaming imported types and namespaces | using NewType1 = Some.Namespace.Type1 and using NewNamespace = Some.Namespace |
09.One-line expression syntax | int triple(int x) => x*3 |
10.Overloading constructors | Person(String name, int age) : this(name) { ... } For subclass, use base() and it enforces constructor in super class is invoked first. |
11.Deconstructor | var (firstName, lastName) = aPersonName (C# 7, see code) |
12.Object initializers | Constructor has an advantage over object initializer in that for immutable object, fields/properties can be declared as read-only and initialized in constructor (see code) |
13.Indexers | string this [int index] { get {...} set {...}} |
14.Static constructors | static PersonName() {...} Acted as static block which get executed once per type after static field initializers. |
15.nameof operator | string foo = nameof (aPersonName) foo will be updated after renaming aPersonName |
16.is operator | Useful to have a casted variable even in an outer scope (see code) |
17.Hiding inherited members instead of override | public new void Foo() {...} (see code) |
18.Initialization order | 1.Fields in subclass, 2.Arguments to base class constructor, 3.Fields in base class, 4.base class constructor, 5.subclass constructor. |
19.virtual, override and sealed | virtual and override make the method in subclass override the one in base class (see code) |
20.reimplement (new) | reimplemented method visible in subclass ONLY, also note without visual subclass can’t override (see code) |
21.explicit implement | It solves method conflict from multiple interfaces by asking caller to cast instance to interface to be able to call the method. Otherwise the implementation is INVISIBLE in the class itself (see code) |
22.class VS interface | Use (sub)classes when they naturally share an implementation; use interface when they have independent implementations. |
23.Covariance VS contravariance | Covariance (out) is generic type used in return value, means IPopable<Animal> obj = instance; // instance is IPopable<Cat> because Cat is instance of Animal. Contravariance (in) is generic type used in parameters, means IPushable<Cat> obj = instance; // instance is IPushable<Animal> because Cat can always be used as method argument when parameter type is Animal (see code) |
24.delegate as Func and Action in System namespace | delegate acts as class (generated by compiler) captured surrounding variables, used like public delegate void Foo(); Foo aDelegate = methodName, aDelegate += anotherMethodName Note Foo can be declared by System.Action, which is delegate void Action(); . Also note delegate can be multi cast. |
25.Standard event pattern | 1.MyEventArgs : System.EventArgs, which is marker class. 2.Use generic delegate System.EventHandler and method to fire event (see code) |
26.Variables captured by delegate | Not evaluated until the delegate is actually invoked. (see code) |
27.Local method VS lambda expression | int Cube(int x) => x*x*x is local method is better in that 1.can be recursive, 2.slightly faster |
28.catch clause | 1.No exception variable, 2.Omit type, 3.exception filter (see code) |
29.throw expression | Since C# 7, throw can be as expression like string Foo() => throw new NotImplementedException(); |
30.Trap of rethrow | To rethrow the original exception (with StackTrace kept), throw instead of throw ex . Rethrow can be used not to leak technical details by rethrowing a less specific exception. |
31.TryXXX pattern | Have a TryXXX method and let XXX method call TryXXX (see code) |
32.foreach translation | It uses enumerator (instead of enumerable) to travel through (see code) |
33.Collection initializer | Two kinds: normal and indexed (see code) |
34.Iterator | With return value as IEnumerableyield return t (or yield break ) inside a method and compiler translates the method into MoveNext method and Current property. |
35.Iterator and try…catch and IDisposable | yield return doesn’t work well with try…cach, only try…finally. Also note foreach can dispose enumerator in case of early break. For explicitly using enumerator (and having early break), wrap it with using . |
36.Nullable | It works for value type (struct) to have a way to define “empty” and compiler can lift operators to nullable from underlying value type (see code) |
37.Cast null to a specific nullable | int? i = (int?)null |
38.Get caller’s info | System.Runtime.CompilerServices (see code) |
39.string’s == and Equals | immutable string’s == operator has been overloaded to behave like its Equals method. |
40.GetHashCode for more than two fields | (see code) |
41.CompareTo and Equals for case-insensitive string | Begin with if (Equals(other)) return 0; for CompareTo so that equal strings always return early. |
42.Functional Construction | Build structural objects (see code) |
43.Disposable pattern | (see code) |
44.Null conditional operator and index operator | ICouldBeNull?[0] == 'S' Apart from . , index operator can come after null conditional operator. |
1 | // 03 |