Artigo em português -> http://felipeaugusto-developer.blogspot.com.br/2013/08/criando-indexadores-c.html
After all, what's indexers in C#?
It's only a intelligent array that can be used both classes as structures, an example:
Let's create our own indexer.
//creating the class
public class Vehicle
{
private Dictionary<string, string> _parts = new Dictionary<string, string>();
//creating the indexer (the "this" is a reference for the class)
public string this[string key]
{
get { return _parts[key];}
set { _parts[key] = value; }
}
//After instantiating the Vehicle, let's use the indexer:
var car = new Vehicle();
car["fabricante"] = "Honda";
car["modelo"] = "Civic";
car["year"] = "2011";
}
Remember that the name of the Key can be anyone, for example, put the name car["indexer"]="value";
If you want to return the value of some property indexed, just do this:
Console.WriteLine("Constructor {0}", car["fabricante"]);
Hope that you enjoyed! =P
Nenhum comentário:
Postar um comentário