日期:2014-05-20 浏览次数:21045 次
if (x in (1, 2, 3)) if (x in 1:5) if (x between(1,5))
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();
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:
...
}
switch (aString)
{
case "one", "two":
Console.WriteLine("1 or 2");
break;
case "three":
Console.WriteLine("3");
break;
default:
...
}
switch (aString)
{
case .IsNullOrEmpty():
...
case .Length > 100:
...
case .Contains("foo"):
...
}
Void DoX(MyClass! obj) { … }
// The following would make the compiler say:
// "Cannot convert string to string!, an explicit conversion exists
string! nonNullable = someFunctionThatReturnsAString();
DelegateType! DelegateVar;
// 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();