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 操作符

C# code?
1
2
3
if (x in (1, 2, 3)) 
if (x in 1:5)
if (x between(1,5))

- 学python的,更加简洁自然的表达式。

2. 数据结构的增强
(1) 一些BCL(base class library)的Collection类型都变成泛型集合
    比如:ControlCollection, XmlAttributeCollection, SqlErrorCollection, StringCollection
    变成:Collection<Control>, Collection<XmlAttribute>,Collection<SqlError> 等
    这使得遍历这种集合的时候,可以直接用 foreach(var x in ...) 目前虽然实现了迭代,但需要类型声明
(2) Tuple类型的自动装箱,拆箱——看上去就像返回多值一样。
 

C# code?
1
2
3
4
5
6
7
8
9
    
 public Tuple<stringintdouble> 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(); 
  

(3) yield foreach (详见后面的嵌套迭代子)

3. switch 语法增强
(1) 智能推测case表达式:比如当switch变量是integer时,允许list,range,甚至是表达式

C# code?
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
            ... 
 

(2) 支持更多的类型,同时支持case中使用list
 

C# code?
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
            ... 
 

(3) 允许在switch语句块中,省略对象访问成员(类似VB.NET的With...End With)

C# code?
1
2
3
4
5
6
7
8
9
10
  
    switch (aString) 
    
        case .IsNullOrEmpty(): 
            ... 
        case .Length > 100: 
            ... 
        case .Contains("foo"): 
            ... 
     

4. Null 安全
(1) 不可为空操作符

C# code?
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();

(2) 返回值不可为空的代理,并支持编译检查

C# code?
1
DelegateType! DelegateVar;

(3) "?."类成员Null安全的操作符: 你可以省掉大量的非空判断

C# code?
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(); 

(4) "???" 空对象链判断结合操作符:从顶级对象判断是否为null
    就像 ?(a.B.C) 如果a==null则返回null

C# code?
1
2
3
MyClass value = null
int something = value.x.y ??? 0;
//something is now 0 

(5) IfNull 和 IfNotNull 关键字,使得非空短路判断更加紧凑

C# code?
1
2
3
4
// instead of
if (a != null && a.SomeProperty != null  && a.SomeProperty.SomeField != null). 
// do this:
IfNotNull(a.SomeProperty.SomeField)  

5. 更强大的泛型约束
(1) 算法类型的约束?(这个没看懂...难道说是T必须都是数值类型?)

C# code?
1
2
3
public T Foo<T>(T blah) where T : number { 
  return blah * blah; 

(2) 枚举类型的约束(目前只支持约束到struct)

C# code?
1
public void DoSomething<T>(T enum) where T : System.Enum { ... } 

(3) 操作符约束,即约束T必须都重载了指定的操作符

C# code?
1
public static int Sum<T>(IEnumerable<T> seq) where T : operator(T=T+T){ .. } 

(4) 带参数构造方法约束

C# code?
1
where new(intstring)

(5) 约束可以调用某个静态方法?(这个感觉不靠谱,不如允许接口里定义static方法)

C# code?
1
2
3
4
public T Create<T>() where T : static Create() 
     return T.Create(); 

(6) 可以约束代理
(7) 可以区别class还是interface

C# code?
1
2
3
4
Derive from T:
class Foo<T> : T where T : class 
Constraint for interfaces:
class Foo<T> where T : interface 

6. 自动属性的增强
(1) 初始值

C# code?
1
public string Name { getset; } = "some value"

(2) readonly声明:只能在构造方法中初始化

C# code?
1
public int SomeValue { getprivate readonly set; } 

7. Dynamic的扩展
(1) 更像javascript,使得不用反射就能获得后期绑定的特性

C# code?
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(); 

(2) 匿名类的属性是只读的,除非通过反射才能修改
希望能像下面:

C# code?
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"

8. 不可变类型
现在只有一个办法封装一个不可变属性(这个不可变还指对属性的公开类成员也不可修改)
比如:使用 ReadOnlyCollection<T> 或者自己包装 ICollection<T> 或者使用 Tuple。
就像 Nullable<T>(?) 一样声明 Immutable<T>(#) 声明属性是不可修改。

C# code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Foo 
  public int ID { getset; } 
  public string Name { getset; } 
 
.. 
  
private Foo# _member; 
  
public Foo# Something 
  get   {  return _member; } 

注意:Foo类型的公开成员ID和Name是可以Set的,但Foo#声明时,就不可以对ID,Name进行修改,这是最终目的。

9. 对于递归的嵌套迭代子
更加简洁有效的递归迭代

C# code?
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(); 
}

* 这里原文直接用 yield base.Foo(); 后面回复有人指出有歧义,我也觉得用yield foreach好理解

10. 特性的增强
(1) lambda表达式可用于特性的参数
(2) 支持泛型的特性
(3) Make attributes first class citizens by allowing any type of parameter type.
    (T___T 这真没看懂)

C# code?
1
2
[SomeCoolAttribute(s=>s.Foo)] 
public string MyProp { getset; }
C# code?
1
11. Enum的扩展

(1) 增加  ToDictionary<Tk,Tv> 和 ToList 扩展方法
(2) 类型化的枚举 (不靠谱,毕竟Enum是ValueType的)

C# code?
1
2
3
4
5
Enum<String>  Options
    Option1 = "xxx" 
    Option2 = "yyy" 

12. Property Change Notification
把 INotifyPropertyChanged 接口实现作成语法糖了?

C# code?
1
public Notifyable<int> Foo { getset; } 

13. 静态方法
(1) 静态的扩展方法 (目前的扩展方法,作用于对象实例上)
(2) 静态的抽象或者虚方法

14. Exception grouping
允许分组捕获异常避免重复的逻辑处理

C# code?
1
2
3
4
5
6
7
8
try 
catch (ArgumentOutOfRangeException) 
catch (ArgumentNullException) 
   // Catch a ArgumentOutOfRangeException or a ArgumentNullException 

15. CsharpRepl
允许C#编译器使用一个默认的类,入口主方法以及默认的命名空间

C# code?
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(); 

16. 事件
使用一个 Fire 关键字来触发事件,Fire关键字的作用是只当事件有订阅者时才真正调用。
   

C# code?
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(thisnew EventArgs<string>(applicationText)); 
  
    // do this
    Fire(this.ApplicationTextUpdated(thisnew EventArgs<string>(applicationText)); 

17. OOP的增强
(1) 鸭子类型
(2) Tuple MustDispose
(3) Binding Contract - 指定源属性和对象属性。(不知道是不是指 ":=:")
(4) Typedef 预定义 (C++有)
(5) methodof/propertyof
(6) Roles -- 或者叫做"traits"而它不是指多重继承
    比如:一个人他可能是“演员”也可能是“爸爸”在不同场景下有不同的角色
(7) make void first class (不懂T_T)
(8) 方法返回匿名类的强类型支持。。。

http://bbs.csdn.net/topics/370033789

老外畅想C# 5.0这个可以有的更多相关文章

  1. MARKDOWN--介绍http://www.jianshu.com/p/q81RER

    简       注册登录 添加关注 作者 简书2013.04.22 22:02* 写了267022字,被8398人关注,获得了9900个喜欢 献给写作者的 Markdown 新手指南 字数1600 阅 ...

  2. 工作记录--使用FFmpeg将一个视频文件中音频合成到另一个视频中

    由于工作需要,临时被老大吩咐去研究一个FFmpeg工具,通过linux命令行去将一个视频中的音频提取出来并合成到另一个视频中,最终的效果是要保证2个视频中的音频都在一个视频中播放. 但是本人对FFmp ...

  3. ZAM 3D 制作简单的3D字幕 流程(二)

    原地址:http://www.cnblogs.com/yk250/p/5663907.html 文中表述仅为本人理解,若有偏差和错误请指正! 接着 ZAM 3D 制作简单的3D字幕 流程(一) .本篇 ...

  4. ZAM 3D 制作3D动画字幕 用于Xaml导出

    原地址-> http://www.cnblogs.com/yk250/p/5662788.html 介绍:对经常使用Blend做动画的人来说,ZAM 3D 也很好上手,专业制作3D素材的XAML ...

  5. 微信小程序省市区选择器对接数据库

    前言,小程序本身是带有地区选着器的(网站:https://mp.weixin.qq.com/debug/wxadoc/dev/component/picker.html),由于自己开发的程序的数据是很 ...

  6. osg编译日志

    1>------ 已启动全部重新生成: 项目: ZERO_CHECK, 配置: Debug x64 ------1> Checking Build System1> CMake do ...

  7. mono3.2和monodevelop4.0在ubuntu12.04上两天的苦战

    首先第一步是设置ubuntu server 12.04版更新源,推荐中科大的比较快:deb http://debian.ustc.edu.cn/ubuntu/ precise main multive ...

  8. EntityFramework 7.0之初探【基于VS 2015】(十)

    前言 本篇作为EF 7.0的开篇也是Entity Framework目前系列末篇,因为关于EF 7.0学习资料实在是太少,我都是参考老外的资料花费了不少时间去研究去尝试同时也失败多次,个人觉得那是值得 ...

  9. Disruptor-net 3.3.0

    Disruptor 介绍 Disruptor 是LMX开源出来的java编写的一个并发消息处理器,在队列中一边生产者放入消息,另外一边消费者并行取出处理,其核心是根据现代CPU硬件缓存特点发明不同于通 ...

随机推荐

  1. Agilent RF fundamentals (10) Mixer ,Phase domain and modulator

    1 Mixer characterization DC input Bias voltage Bias Current RF input Lo input IF output 2 mixer devi ...

  2. Celery分布式应用

    最近有应用需要部署到不同的服务器上运行,但是有没有PBS这样的调度系统,就想起来Python的调度神器 Celery.现在针对我的实际应用做一些记录. 1. 安装 因为我并不注重结果而是把命令拿到不同 ...

  3. android代码设置EditText只输入数字、字母

     如何设置EditText,使得只能输入数字或者某些字母呢? 一.设置EditText,只输入数字: 方法1:直接生成DigitsKeyListener对象就可以了. et_1.setKeyLis ...

  4. Javascript+CSS实现影像卷帘效果

    用过Arcgis的筒子们对于Arcmap里面的一个卷帘效果肯定记忆很深刻,想把它搬到自己的WebGIS系统中去,抱着同样的想法,我也对这种比较炫的卷帘效果做了一下研究,吼吼,出来了,给大家汇报一下成果 ...

  5. 关于json解析和所需jar

    以解析字符串数组为例: String parameter= [{"id":"pdTNKcY1YS55oG2M",.......}] 1. 关于net.sf.js ...

  6. c++ 基础知识 0001 const 知识1

    1. C++ const用法 尽可能使用const 2. C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不变的.如果在编程中确实有某个值保持不变,就 ...

  7. not

    x = [] print(x) print(not x) print(x is None) print(not x is None) print(x is not None)

  8. 阿里云部署django实现公网访问

    本博的主要目的是对阿里云部署django实现公网访问进行一次简单的记录,方便日后查询. 内容目录: (1)申请阿里云服务器及安全组配置 (2)实现ssh远程控制 (3)实现ftp文件传输 (4)安装p ...

  9. NPOI时间格式判断

    switch (cell.CellType) { case CellType.BLANK: //空数据类型处理 dr[iRow] = ""; break; case CellTyp ...

  10. Chrome 的审查元素功能有哪些奇技淫巧

    学习地址: https://www.zhihu.com/question/34682699