111,077
社区成员




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();
MyClass value = null;
int something = value.x.y ??? 0;
//something is now 0
// instead of
if (a != null && a.SomeProperty != null && a.SomeProperty.SomeField != null).
// do this:
IfNotNull(a.SomeProperty.SomeField)
public T Foo<T>(T blah) where T : number {
return blah * blah;
}
public void DoSomething<T>(T enum) where T : System.Enum { ... }
public static int Sum<T>(IEnumerable<T> seq) where T : operator(T=T+T){ .. }
where new(int, string)
public T Create<T>() where T : static Create()
{
return T.Create();
}
Derive from T:
class Foo<T> : T where T : class
Constraint for interfaces:
class Foo<T> where T : interface
public string Name { get; set; } = "some value";
public int SomeValue { get; private readonly set; }
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();
var obj = new dynamic
{
Foo = 1,
Bar = "string"
};
obj.Foo = 2;
obj.NewProperty = Something();
obj["Bar"] = "a new value";
class Foo
{
public int ID { get; set; }
public string Name { get; set; }
}
..
private Foo# _member;
public Foo# Something
{
get { return _member; }
}
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();
}
[SomeCoolAttribute(s=>s.Foo)]
public string MyProp { get; set; }
11. Enum的扩展
Enum<String> Options
{
Option1 = "xxx"
Option2 = "yyy"
}
public Notifyable<int> Foo { get; set; }
try
{
}
catch (ArgumentOutOfRangeException)
catch (ArgumentNullException)
{
// Catch a ArgumentOutOfRangeException or a ArgumentNullException
}
// 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();
// 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));