suppose I have a class like
class ABC
{
int ID;
public string Name;
public ABC(int i, string str) { ID = i; Name = str; }
}
And declare a array of class ABC.
Assume that I have also initialized each element using new keyword.
Now I want access element of class using it’s member ID instead of 0 based index. Make ID public if required.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ABC[] a = new ABC[3];
a[0] = new ABC(100, "First");
a[1] = new ABC(101, "Second");
a[2] = new ABC(102, "Third");
ABC newobj = a[100]; // where 100 is ID of class ABC
}
}
class ABC
{
int ID;
string Name;
public ABC(int i, string str)
{
ID = i;
Name = str;
}
}
Overloading the indexer on a standard array isn’t possible in C#. If you have to go the indexer route I’d suggest using Dictionary or a List class that supplies it’s own indexer.
Then you call use ABCCollection[100] to access your item.