【发布时间】:2015-07-11 18:03:40
【问题描述】:
这三种方式有什么区别:
Application xlApp = new Application();
Workbooks xlWorkbooks = xlApp.Workbooks;
Workbook xlWorkbook = xlWorkbooks.Open(filePath);
Sheets xlSheets = xlWorkbooks.Sheets;
// 1. Way
Worksheet xlSheet = xlSheets["SheetName"] as Worksheet;
// 2. Way
Worksheet xlSheet = xlSheets.Item["SheetName"] as Worksheet;
// 3. Way
Worksheet xlSheet = xlSheets.get_Item("SheetName") as Worksheet;
1.方式被描述为
索引器对象 Microsoft.Office.Interop.Excel.Sheets.this
保留供内部使用。
2. 方式:
索引属性对象 Microsoft.Office.Interop.Excel.Sheets.Item
从集合中返回单个对象。
还有 3. 方式:
[对象索引]:对象
(对于这种方式,ReSharper推荐使用2.方式。)
那么,所有 3 种方法都完全相同,但是推荐哪一种,有什么区别? 我认为所有的 COMObject Collections 都有这 3 种可能性。
【问题讨论】: