【有稽之谈】C# 5.0 这个可以有
最近看了这篇文章:
C# 5.0 - not quite there yet!
老外大胆的YY了一下,感觉挺有意思转发过来。
回顾C#发展的历史,C#1.0模仿了Java,并保留了C/C++的一些特性如struct,新学者很容易上手;
C#2.0加入了泛型,匿名方法,yield关键字(为啥VB.NET到现在还没有yield?);
C#3.0加入了一堆语法糖,lambda,linq的加入,让C#变得更加优雅和灵活;
C#4.0增加了动态语言的特性,另外加入的TPL(并行开发库),PLinq等降低现存并发模型的复杂性也是相当的给力。
C#5.0???? 还会有什么奇思妙想?
1. in 和 between 操作符
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 existsstring! 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();
就像 ?(a.B.C) 如果a==null则返回null
MyClass value = null; int something = value.x.y ??? 0;//something is now 0
// instead ofif (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));
[解决办法]
能在现在基础上做些语法增强。。
我就感觉很牛B了。。
[解决办法]
看过,支持。
[解决办法]
呵呵 ~
[解决办法]
要是再优化下去,程序员这个岗位可能就要消失在历史中了。。。。。。。。
[解决办法]
null安全,这在目前简直是个陷阱。
[解决办法]
越看越感觉自己什么都不会
[解决办法]
学习了~
[解决办法]
学习了
[解决办法]
好程序
[解决办法]
期待更强大的泛型约束,非常有用。
[解决办法]
顶一个
[解决办法]
非常感谢。。学习了。。
[解决办法]
不希望有太多傻瓜化的语法
该自己判空还是得自己来
[解决办法]
你是搞不懂微软想什么的
[解决办法]
呵呵,来凑个热闹
[解决办法]
YY中,迫切希望C#5中会出现
[解决办法]
C# 5.0出来吗?
[解决办法]
学习了~
[解决办法]
纯属扯淡,无语
[解决办法]
人创造东西 就是为了偷懒..........人的惰性 激发了创造力...
[解决办法]
程序员还有用么?
[解决办法]
微软常做表面的文章占领市,如Windows脸越来越漂亮,Office也越来越方便,而现在语言也越来越多的糖...可是性能确不被重视.....
[解决办法]
还在用2.0 = =
[解决办法]
测试过哪些系统?.net4.0比.net2.0慢么?
[解决办法]
这些功能太YY了
[解决办法]
路过,学习了。。。
[解决办法]
挺好,学习了。
[解决办法]
学习了!不错
[解决办法]
支持。现在感觉有些语法的确用起来不太简洁。
[解决办法]
学习了~~~
[解决办法]
飘过~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[解决办法]
确实要学习学习,顶一下!
很有意思!
[解决办法]
[解决办法]
很好很强大,I like
[解决办法]
这个可以有!好强大
[解决办法]
Make attributes first class citizens by allowing any type of parameter type.
很原汁原味的语言
意思大致是,使得attributes成为类里面的头等公民(市民)允许任何类型的参数类型。
[解决办法]
先看看,貌似挺复杂
[解决办法]