George F DArcy

Please forgive me, I'm quite new to C# and OO.

Can someone please explain what this bit of code is It is quite a bit different from the basic stuff I do!

public class MyClass
{
public MyThing this[int Id]
{
//... some code that returns either a MyThing or null
}
}

I've snipped it a bit, but hopefully there's enough there.

The bit that I dont understand is;

public MyThing this[int Id]

the 'this[int Id]' part.

Is this a constructor If so, I thought the syntax was 'public MyThing' followed by some parameters in ().

thanks

george d




Re: Visual C# General What does this bit of code do?

Jay Vora

its an indexer hvng set n get properties...

http://www.csharphelp.com/archives/archive140.html

but if mything is a function ..then this is for the home class myclass






Re: Visual C# General What does this bit of code do?

Friendly Dog

MyThing is not a function, it's a type.

This piece code defines a indexer for MyClass class and it returns a MyThing instance contained in MyClass instance by the specified index.






Re: Visual C# General What does this bit of code do?

Matthew Watson

To add to what the others said:

Having an "indexer" for a class means that you can access items in an instance of that class as if it were an array:

MyClass myClass = new MyClass(...)
...
myClass[1] = ... // Access it as if it were an array.