#### Archived

This topic is now archived and is closed to further replies.

# Could someone explain Indexers to me [c#]

This topic is 5198 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

As far as I can tell, indexers allow a programmer to do something like the following: MyClass MC = new MC(); MC[0] = "Whats up?"; MC[1] = "Yo"; How is this any different than doing this: MyClass[] MC = new MC[4]; //an array of classes, forgive me if the syntax isnt exactly right Does using Indexers have the advantage of increasing the size of the array if necessary, or what? Im really confused. For anyone that wants to know, Im following the tutorials at C# Station.

##### Share on other sites
quote:

MyClass[] MC = new MC[4]; //an array of classes, forgive me if the syntax isnt exactly right

What that does is create a static array of instances of MyClass.

BTW-The new MC should be new MyClass.

You would reference each member of the array like:
MC[0]
MC[1]
MC[2]
MC[3]

and they would be of type MyClass.

quote:

MyClass MC = new MC();
MC[0] = "Whats up?";
MC[1] = "Yo";

That is an example of using indexers. Indexers are class specific so there is no way for me to know what that does. You could define an indexer to take a string or any other variable type and return any type of variable you wanted. You can use indexers to do whatever you want them to because there user defined. That probably didn''t make much sense so if you have any questions, I or someone else will be happy to try to answer it in a better manner

##### Share on other sites
Im sorry, but I still dont understand just what Indexers are and how they are used, but no explanation. Thanks anyway.

However, just how important are indexers? The whole reason I began learning C# was to work with GUIs for a while (tired of console in C++, Win32 was too hard), and I discovered that I actually like the way the language works. Well, I thought that maybe I could skip indexers for now if they arnt that important, and as I learn more about C#, GUI programming and the framework I could come back to them. Does that sound okay?

Thanks.

##### Share on other sites
In delphi you can define a class as eg.
type  myclass = class    posx,posy: integer;    name: string;    inventory: array[1..20] of Tmyitem; default;    invcount: integer;  end;// in the code  mything := myclass.Create;  mything.posx := 100; mything.posy := 200;// These are the same:  inc(invcount);  mything.inventory[invcount] := sword;  inc(invcount);  mything[invcount] := shield;

##### Share on other sites
I''ll follow up with another example (in Delphi again). A listbox is available in the TListbox control in Delphi. Each listbox has a property called "Items", which lets you get a particular item from it. For example:

SomeListbox.Items[0] := ''some item'';SomeListbox.Items[1] := ''another one'';

That''s easy enough, isn''t it? Well, indexers are simply short-hand for the above , in a way. So, for a hypothetical indexer for a Delphi TListbox (I don''t actually think it has one, but for argument''s sake)...

SomeListbox[0] := ''some item''; // same as SomeListbox.Items[0]

See? Just short-hand, nothing complicated. (Assuming it''s like that in C#, which it should be because C# seems to have a lot of similarities for obvious reasons).

##### Share on other sites
Take a look at the ArrayList and Hashtable classes. They show a good example of what to use indexers for.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

##### Share on other sites
hrmm... Always wondered what delphi code looked like. Now I know... Looks kinda cryptic.. :-)

Basically indexers allow you to control what the [] in C# does. You could use it to simplify the syntx and allow someone to do something like:

ObjectList["Tank1"]

to refrence an object in a list(assuming you were making a game that had a list of objects in it) instead of having to do something like this:

ObjectList->GetObject("Tank1");

In case you don''t read delphi :-), here is my rough translation of the above code(I don''t read it either so...):

enum ItemType{   sword,   shield}class myclass{    public ItemType this[int index]    {        get         {            return inventory[index];        }        // Write one byte at offset index and return it.        set         {            inventory[index] = value;        }    }    private ItemType[] inventory = new ItemType[20];    public int posx;    public int posy;    public int invCount;}In the code:myclass mything = new myclass();mything.posx = 100;mything.posy = 200;// Uses an indexer to simply thingsmything.inventory[0] = sword;

BTW-I didn''t compile that code so I have no idea if it contains silly errors or not...