C♯ 4.0 的特性:修订间差异
来自站长百科
(新页面: <span style="text-align:center;border:1px solid #000;padding:5px;float:right;"><strong>导航:</strong>上一页|{{Template:程序设计语言}}</span> <div ...) |
Ahbaybface(讨论 | 贡献) 无编辑摘要 |
||
第38行: | 第38行: | ||
IEnumerator<T> GetEnumerator(); | IEnumerator<T> GetEnumerator(); | ||
} | } | ||
<pre> | </pre> | ||
==相关条目== | ==相关条目== | ||
*[[C♯ 2.0 的特性]] | |||
*[[C♯ 3.0 的特性]] | *[[C♯ 3.0 的特性]] | ||
==参考来源== | ==参考来源== |
2010年10月25日 (一) 11:41的最新版本
导航:上一页|C语言 | C++ | C♯ |JAVA | Perl | Pascal | Visual Basic
动态查阅[ ]
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 时, 顺序可以完全颠倒:
OpenTextFile(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 支持 Co-Variance 与 Contra-Variance,例如在泛型接口可以加上in修饰字:
public interface IComparer<in T> { public int Compare(T left, T right); } public interface IEnumerable<out T> : IEnumerable { IEnumerator<T> GetEnumerator(); }