Why does C# have indexers and Visual Basic does not?

C

COHENMARVIN

I'm a VB.net programmer learning C#. I don't understand what indexers
are for in C#. Can't you have arrays of classes that can be indexed
in both C# and VB.net? And if they contain arrays as members, can't
those arrays be indexed?
What doesn't VB have indexers?
Thanks?
-- Marvin
 
J

Jon Skeet [C# MVP]

I'm a VB.net programmer learning C#. I don't understand what indexers
are for in C#. Can't you have arrays of classes that can be indexed
in both C# and VB.net? And if they contain arrays as members, can't
those arrays be indexed?

Yes, you can index arrays - but you can also index things other than
arrays, such as lists and dictionaries.
What doesn't VB have indexers?

It does really - it just thinks of them as parameterised properties.

For instance, if you do:

myList.Item(i)

in VB.NET, that's the equivalent of

myList in C#.
 
N

Nicholas Paldino [.NET/C# MVP]

Marvin,

What do you mean when you ask if those classes can be indexed?

Indexers are just a shorthand for accessing collection classes by some
sort of key. By default, the indexer in C# produces a property named Item
which can be used to access the values by that key in languages that don't
support indexers.

You might want to take a look at this thread which shows a little more
information on how indexers are represented in IL, which is what C# takes
its cues from when creating/accessing indexers:

http://groups.google.com/group/micr...6163602153a/f2ccef7358a1c9a3#f2ccef7358a1c9a3
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,



I'm a VB.net programmer learning C#. I don't understand what indexers
are for in C#.

An index let you call a "default property" using an index:
DataView dv = new DataView(.....)

dr[1];

instead of using dv.Items[1];
Can't you have arrays of classes that can be indexed
in both C# and VB.net?

You lost me there, what you mean with "be indexed" ?
 
C

COHENMARVIN

Hi,


I'm a VB.net programmer learning C#. I don't understand what indexers
are for in C#.

An index let you call a "default property" using an index:
DataView dv = new DataView(.....)

dr[1];

instead of using dv.Items[1];
Can't you have arrays of classes that can be indexed
in both C# and VB.net?

You lost me there, what you mean with "be indexed" ?

Never mind the "be indexed", my question was confused, but you people
have answered it.
-- CM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top