Range [] instead of get_Range ()

http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.get_range.aspx says that to use the Range property instead of get_Range (Object Cell1, Object Cell2).

They both do the same. Gets a Microsoft.Office.Interop.Excel.Range object that represents a cell or range of cells . So what's the difference besides being a method and another a property? Why do they indicate the use of Range [], what is the reason for this?

+7
source share
3 answers

Range () is faster than Range []

In practice, we noticed this. But here the reason should be indicated.

This shortcut is convenient if you want to refer to the absolute range. However, it is not as flexible as Rangeproperty, since it cannot handle variable input as strings or object references. Therefore, at the end of the day you will still return to the long road. Although shorty provides readability. Consequently, perhaps this will be correct in the first round without the additional cost of resources.

Now why is it slow? When compiling.

"At runtime, Excel always uses standard notation (or, as I was told), so when the code compiles, all links in a note note should be converted to the usual range form (or so I was told). {Ie [A150] should be converted to Range ("A150") form. Regardless of the truth of what I was told, Visual Basic should remember both its compiled version of the code and any notation you used to write your code (i.e. whatever in the code module), workbook properties for file size (used memory), such as again slightly increased. "

As you can see, my answer was more compatible with VBA. However, after some research it has been proven that the VBA side does not slow down much. Therefore, you only need to take care of the C # side. @Hans gives you the best answer in a C # perspective. Hope combines the fact that you get a great code :)

Here is some performance information on Range [] vs Range () in Excel

enter image description here

+6
source

If you are using C # version 4 and above, you can use the Range indexer. But you must use get_Range () in earlier versions.

Notice that there is something special, the default property of the COM interface is mapped to an index. But the Range property is not the default property for Worksheet, it is just a regular property. The problem is that C # does not allow you to declare indexed properties other than an indexer. It works in VB.NET and not in C #, you had to call the getter method of the property directly. By popular demand, the C # team dropped this limitation in version 4 (VS2010). But only on COM interfaces, you still cannot declare indexed properties in your own code.

+3
source

I used both and both returned the same results. I think Range [] actually uses get_Range () inside.

For the naming question, I use only Range [].

0
source

All Articles