7

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

They are both doing the same thing, Gets a Microsoft.Office.Interop.Excel.Range object that represents a cell or a range of cells. So, what's the difference except that this is a method and another is a property? Why are they pointing on use of Range[], what's the reason for it?

4
  • Maybe because it doesn't respect C# naming convention...
    – digEmAll
    Dec 7, 2012 at 9:01
  • is it really because of that or is it just your opinion? if you can just to elaborate that cause I'm really interested in this. than you
    – Sylca
    Dec 7, 2012 at 9:33
  • 1
    On second thought, get_PROPERTYNAME is the way C# handles properties under the hood (basically properties are converted into get and set methods with that syntax). Usually they're not available at compile time and you need to call the Property in the usual way, but in this case (for some reason) it is available. So they suggest to use the Property syntax cause is more correct (but I think they're exactly the same).
    – digEmAll
    Dec 7, 2012 at 10:18
  • +1 for nice question, made me confirm few things.
    – bonCodigo
    Dec 7, 2012 at 18:33

3 Answers 3

6

Range() is faster than Range[]

By practice we have noticed it the case. But here should define a reason to say so.

This shortcut is convenient when you want to refer to an absolute range. However, it is not as flexible as the Rangeproperty as it cannot handle variable input as strings or object references. So at the end of the day you will still end up referring the long way. Although the shorty provides readability. Hence might as well get it right the first round without more resources spending.

Now why is it slow? In the compiling.

"During run-time Excel always uses conventional notation (or so I've been told), so when the code is being compiled all references in shortcut notation must be converted to conventional range form (or so I've been told). {ie [A150] must be converted to Range("A150") form}. Whatever the truth of what I've been told, Visual Basic has to memorize both its compiled version of the code and whatever notation you used to write your code (i.e. whatever's in the code module), the workbook properties for the file size (the memory used) thus goes up slightly. "

As you see my answer was more in line with VBA. However after some research it is sort of proved that VBA side doesn't do much slowing down. So you only need to take care of the C# side. @Hans gives you a better answer in C# perspective. Hope combining both that you will get a great performing code :)

Here is some finding on the performance of Range[] vs Range() in Excel

enter image description here

5
  • I doubt @bonCodigo asks a question. bonCodigo, any source this is faster, and how much faster?
    – RvdK
    Dec 7, 2012 at 11:32
  • @bonCodigo I'll be glad just to see your code sample of your answer. By the way, I'm hopping that the concentration will be on C# rather than on VB?!:-)
    – Sylca
    Dec 7, 2012 at 13:12
  • @Sylca if I didn't see a second great article, even I would have been in dark, not totally dark but little cloudy anyway ;-) This has to be my favourite Excel article of the month. MVP is talking about performance of our very topic here. So the slowness of shortcut is mainly quite small that you could ignore intact given the flexibility you get via range[]. I wish I could get more deeper level info. For now you have a choice :-) shortcut range reference
    – bonCodigo
    Dec 7, 2012 at 16:54
  • Thank you for your effort! The excelhero.com/blog/2010/06/when-working-in-vba-we.html gave me a bigger picture about some things. This simple statement to me was enough for an answer, although its word about VBA. "Now I will warn you upfront that the shortcut method is never the quickest, but for me the clarity and simplicity it provides usually wins out. And truth be told, the speed difference is modest. But most of the time the tiny speed difference is irrelevant."
    – Sylca
    Dec 10, 2012 at 7:54
  • Precisely that was the statement changed my mind to not to object to Range[] usage anymore :) However, personally I will stick to Range() ;)
    – bonCodigo
    Dec 10, 2012 at 8:02
3

If you use C# version 4 and up then you can use the Range indexer. But you have to use get_Range() on earlier versions.

Do note that there's something special about it, the default property of a COM interface maps to the indexer. But the Range property is not the default property of a Worksheet, it is just a regular property. Trouble is, C# does not permit declaring indexed properties other than the indexer. Works in VB.NET, not in C#, you had to call the property getter method directly. By popular demand, the C# team dropped this restriction in version 4 (VS2010). But only on COM interfaces, you still cannot declare indexed properties in your own code.

3
  • Personally, I'm using Range[] indexed property rather than get_Range() method mostly cause it looks kind of cleaner to me and also I've seen RSharper proposing the use indexed property. That also made me asking myself the question that I've posted here.
    – Sylca
    Dec 7, 2012 at 13:23
  • 2
    That's fine of course, point is that you can't when you use an old version of VS. Which is why there are two ways to do it, the crux of your question. Dec 7, 2012 at 13:43
  • 1
    Thank you for not letting me astray from crux of my question.
    – Sylca
    Dec 7, 2012 at 21:59
0

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

For a question of naming convention I only use Range[] now.

1
  • I agree with you, I've also used both of them and they both return the same. Do you think that maybe one of them is faster?
    – Sylca
    Dec 7, 2012 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.