C# 4.0的特性 总结

动态查阅

C# 4.0 新增 dynamic关键字,提供动态编程(dynamic programming),把既有的静态对象标记为动态对象,类似javascript, Python 或 Ruby。

dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);


 具名参数与可选参数public StreamReader OpenFile(
    string path,
    int bufferSize = 1024)
{
...
}
调用 OpenFile 时, 顺序可以完全颠倒:

OpenFile(bufferSize: 4096, path: "foo.txt");


 与 COM 对象交互在 C#中打开一个Word文件:

static void Main(string[] args) {
    Word.Application wordApplication = new  
       Word.Application() {Visible = true};    
    wordApplication.Documents.Open(@"C:\\plant.docx",  
       ReadOnly: true); 
}
在C#中指定Excel的某一格文字:

excelObj.Cells[5, 5].Value = "This is sample text";


泛型的协变和逆变C# 4.0 支持协变和逆变,例如在泛型接口可以加上in、out关键字。

  public interface IComparer<in T> 
  { 
    public int Compare(T left, T right); 
  }
 
  public interface IEnumerable<out T> : IEnumerable
  {
    IEnumerator<T> GetEnumerator();
  }

这里做简单的总结, 当然还有一些,3.0,4.0语法糖有很多, 同时mvc ,sl,wcf 也是.net程序员必备的技能,努力掌握把!

posted @ 2011-07-29 10:52  JavAndroidJSql  阅读(267)  评论(1编辑  收藏  举报