Heb ondertussen VS 2015 even in gebruik en ik dacht eens te kijken naar de nieuwe features in C# 6.0. Vooral veel syntactic sugar, maar één feature is wel enorm handig namelijk de nameof operator.
Weer een hoop magic strings kunnen replacen
. Vooral de enorme hoeveelheid strings in PropertyChanged event calls irriteerde me al lang.
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| using Stuff = Some.Cool.Functionality class C { static int Method1 (string x, int y) {} static int Method1 (string x, string y) {} int Method2 (int z) {} string f<T>() => nameof(T); } var c = new C() nameof(C) -> "C" nameof(C.Method1) -> "Method1" nameof(C.Method2) -> "Method2" nameof(c.Method1) -> "Method1" nameof(c.Method2) -> "Method2" nameof(z) -> "z" // inside of Method2 ok, inside Method1 is a compiler error nameof(Stuff) = "Stuff" nameof(T) -> "T" // works inside of method but not in attributes on the method nameof(f) -> "f" nameof(f<T>) -> syntax error nameof(f<>) -> syntax error nameof(Method2()) -> error "This expression does not have a name" |
Weer een hoop magic strings kunnen replacen
