1
2
3 |
if (x in (1, 2, 3)) if (x in 1:5) if (x between(1,5)) |
1
2
3
4
5
6
7
8
9 |
public Tuple< string , int , double > ReturnMyTuple() { return "Hello World!" , 42, 4.2; } // elsewhere: item1 is a string, item2 is an int, item3 is a double. var item1, item2, item3 = ReturnMyTuple(); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
switch (anInt) { case 1, 2: Console.WriteLine( "1 or 2" ); break ; case 3..9: Console.WriteLine( "3 to 9" ); break ; case >= 10: Console.WriteLine( "10 or higher" ); break ; default : ... } |
1
2
3
4
5
6
7
8
9
10
11 |
switch (aString) { case "one" , "two" : Console.WriteLine( "1 or 2" ); break ; case "three" : Console.WriteLine( "3" ); break ; default : ... } |
1
2
3
4
5
6
7
8
9
10 |
switch (aString) { case .IsNullOrEmpty(): ... case .Length > 100: ... case .Contains( "foo" ): ... } |
1
2
3
4 |
Void DoX(MyClass! obj) { … } // The following would make the compiler say: // "Cannot convert string to string!, an explicit conversion exists string ! nonNullable = someFunctionThatReturnsAString(); |
1 |
DelegateType! DelegateVar; |
1
2
3
4
5
6
7
8
9
10 |
// Instead of doing: var obj = Foo(); Bar value = null ; if (obj.Bar != null && obj.Bar.Something != null ) { value = obj.Bar.Something.DoSomething(); } //You can do this with Groovy‘s null safe member operator ?. var obj = Foo(); var value = obj?.Bar?.Something?.DoSomething(); |
1
2
3 |
MyClass value = null ; int something = value.x.y ??? 0; //something is now 0 |
1
2
3
4 |
// instead of if (a != null && a.SomeProperty != null && a.SomeProperty.SomeField != null ). // do this: IfNotNull(a.SomeProperty.SomeField) |
1
2
3 |
public T Foo<T>(T blah) where T : number { return blah * blah; } |
1 |
public void DoSomething<T>(T enum ) where T : System.Enum { ... } |
1 |
public static int Sum<T>(IEnumerable<T> seq) where T : operator (T=T+T){ .. } |
1 |
where new ( int , string ) |
1
2
3
4 |
public T Create<T>() where T : static Create() { return T.Create(); } |
1
2
3
4 |
Derive from T: class Foo<T> : T where T : class Constraint for interfaces: class Foo<T> where T : interface |
1 |
public string Name { get ; set ; } = "some value" ; |
1 |
public int SomeValue { get ; private readonly set ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
Currently, this would take: var obj = new Foo(); object temp = obj .GetType() .GetProperty(aVariable) .GetValue(obj, null ); int value = ( int )temp .GetType() .GetMethod( "Method" ) .Invoke(temp, null ); What we want: dynamic obj = new Foo(); int value = obj[aVariable].Method(); |
1
2
3
4
5
6
7
8
9 |
var obj = new dynamic { Foo = 1, Bar = "string" }; obj.Foo = 2; obj.NewProperty = Something(); obj[ "Bar" ] = "a new value" ; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
class Foo { public int ID { get ; set ; } public string Name { get ; set ; } } .. private Foo# _member; public Foo# Something { get { return _member; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13 |
public override IEnumerable< int > Foo() { yield return 1; yield return 2; foreach (var i in base .Foo()) yield i; } //allowing me to write: public override IEnumerable< int > Foo() { yield return 1; yield return 2; yield foreach base .Foo(); } |
1
2 |
[SomeCoolAttribute(s=>s.Foo)] public string MyProp { get ; set ; } |
1 |
11. Enum的扩展 |
1
2
3
4
5 |
Enum<String> Options { Option1 = "xxx" Option2 = "yyy" } |
1 |
public Notifyable< int > Foo { get ; set ; } |
1
2
3
4
5
6
7
8 |
try { } catch (ArgumentOutOfRangeException) catch (ArgumentNullException) { // Catch a ArgumentOutOfRangeException or a ArgumentNullException } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 |
// Instead of: using System; class Program { static void Main( string [] args) { Console.WriteLine(fact(10)); Console.ReadLine(); } static int fact( int n) { if (n == 0) return 1; else return n * fact(n - 1); } } // Use this: static int fact( int n) { if (n == 0) return 1; else return n * fact(n - 1); } Console.WriteLine(fact(10)); Console.ReadLine(); |
1
2
3
4
5
6
7
8 |
// Instead of: // copy ref to delegate for thread safety var evt = this .ApplicationTextUpdated; if (evt != null ) evt( this , new EventArgs< string >(applicationText)); // do this Fire( this .ApplicationTextUpdated( this , new EventArgs< string >(applicationText)); |
老外畅想C# 5.0这个可以有,布布扣,bubuko.com
原文:http://www.cnblogs.com/passer/p/3633454.html