I know C# well, but it is something strange for me. In some old program, I have seen this code:
public MyType this[string name]
{
......some code that finally return instance of MyType
}
How is it called? What is the use of this?
I know C# well, but it is something strange for me. In some old program, I have seen this code:
public MyType this[string name]
{
......some code that finally return instance of MyType
}
How is it called? What is the use of this?
It is indexer. After you declared it you can do like this:
This is an Indexer Property. It allows you to "access" your class directly by index, in the same way you'd access an array, a list, or a dictionary.
In your case, you could have something like:
You'd then be able to use this like:
This is a special property called an Indexer. This allows your class to be accessed like an array.
You'll see this behaviour most often in custom collections, as the array-syntax is a well known interface for accessing elements in a collection which can be identified by a key value, usually their position (as in arrays and lists) or by a logical key (as in dictionaries and hashtables).
You can find out much more about indexers in the MSDN article Indexers (C# Programming Guide).
It's an indexer, generally used as a collection type class.
Have a look at Using Indexers (C# Programming Guide).