Reputation: 1987
I don't know how to explain my question, please accept it in example form:
I wrote a library in C# language which has a method as below:
public object GetValueAt(int idx) {
return arr[idx];
}
Then I use it in VB.Net, ofcourse there's a diffirent in index based between C# and VB.Net. So if I call that method with idx = 6
, how does CLR know object which I try to access (it having idx = 5
on C#)?
That's just my example, and what about existed library in .Net?
Upvotes: 1
Views: 162
Reputation: 239814
The only difference I'm aware of where you may believe there's an indexing difference between C# and VB.Net is when declaring an array.
In VB.Net, you declare the upper bound of the array:
Dim x(10) as Int32
declares an array with 11 elements, starting at 0, ending at 10. In C#, you declare the length of the array:
Int32 []x = new Int32[10];
declares an array with 10 elements, starting at 0, ending at 9.
Indexed access with identical index values works the same in both languages.
Upvotes: 6
Reputation: 1503459
If you call that method from VB with idx = 6
it will return arr[6]
treating arr
as 0-based (i.e. the seventh element), because the code is written in C#. No automatic rebasing is applied, because you're just calling a method from VB.
I thought the VB compiler automatically adjusts array indexes when the array indexing expression itself is in VB, but that's not the case here - it's just a method call. EDIT: It looks like that doesn't happen anyway, at least not always. The compiler makes allowances when creating arrays, but not indexing into them, apparently...
EDIT: That chimes in with the MSDN "Arrays In Visual Basic" page which shows an example and explains:
The array students in the preceding example contains 7 elements. The indexes of the elements range from 0 through 6.
And from this blog post:
As many long-time web developers may know, Visual Basic and VB Script used to have 1-based arrays for many years while just about every other language I used -- C, C++, JScript, JavaScript -- had 0-based arrays. I didn't particularly have any strong preference for one over the other, but it was tricky to adjust your mind when switching from one to the other.
Then when .Net was released, Visual Basic changed to using 0-based arrays.
Upvotes: 3