简体   繁体   English

C#Excel VSTO:get_Range()和Range()之间的区别

[英]C# Excel VSTO: Difference between get_Range() and Range()

I am trying to understand the difference between Worksheet.Range() and Worksheet.get_Range(). 我试图了解Worksheet.Range()和Worksheet.get_Range()之间的区别。 Take the two lines of codes below: 请看下面两行代码:

Globals.ThisWorkbook.Worksheets["Sheet1"].Range("A4").Value2 = "test";

This will print "test" on cell A4 on worksheet labelled "Sheet1". 这将在工作表上标记为“ Sheet1”的单元格A4上打印“测试”。 Similarly, 同样的,

Globals.ThisWorkbook.Worksheets["Sheet1"].get_Range("A4").Value2 = "test";

should produce the same result, instead I got: 应该产生相同的结果,相反我得到了:

'System.__ComObject' does not contain a definition for 'get_Range

Can someone explain when and how to use get_Range()? 有人可以解释何时以及如何使用get_Range()吗?

Is there any performance benefit? 有什么性能上的好处?

In C# code, a property X produces methods named get_X and set_X under the hood. 在C#代码中,属性Xget_X生成名为get_Xset_X方法。 Therefore, get_Range("A4") should instead be written as Range["A4"] . 因此, get_Range("A4")写为Range["A4"] See the code in the first example in this MSDN document for an example. 有关示例,请参阅此MSDN文档中第一个示例中的代码。

I doubt you will see any performance difference, though as in most performance questions the answer is "try it and compare times". 我怀疑您是否会看到性能差异,尽管在大多数性能问题中答案都是“尝试并比较时间”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM