c# 4.0新特性一览
2009-03-02 08:17:59 来源:WEB开发网Named and optional parameters
这似乎不是什么很难实现或很新颖的特性,只要编译器的支持就可以(VB很早就支持了)。估计加入的原因是群众的呼声太高了。
带有可选参数方法的声明:
public StreamReader OpenTextFile(
string path,
Encoding encoding = null,
bool detectEncoding = true,
int bufferSize = 1024);
命名参数必须在最后使用:
OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096);
顺序不限:
OpenTextFile(bufferSize: 4096, path: "foo.txt", detectEncoding: false);
Improved COM Interoperability
在C#中在调用COM对象如office对象时,经常需要写一堆不必要的参数:
object fileName = "Test.docx";
object missing = System.Reflection.Missing.Value;
doc.SaveAs(ref fileName,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
4.0中就可以直接写成:
doc.SaveAs("Test.docx");
C#4.0对COM交互做了下面几方面的改进:
Automatic object -> dynamic mapping
Optional and named parameters
Indexed properties
Optional “ref” modifier
Interop type embedding (“No PIA”)
- ››特性信息
更多精彩
赞助商链接