A custom indexer is generally used to customize collection classes and obtain an element in a collection instance.
In addition, C #. Net has a generic set that can be used. Write this article mainly to review some things that are easy to forget.
Namespace aspxdemo_lib
{
Public class productcollection: collectionbase
{
Public Product this [int Index]
{
Set {base. innerlist [Index] = value ;}
Get {return (product) base. innerlist [Index];}
}
Public void add (product)
{
Base. innerlist. Add (product );
}
}
Public class product
{
Public string name {Get; set ;}
Public String site {Get; set ;}
}
}
The new indexer is used in the unit test as follows:
[Testmethod]
Public void indexer unit test ()
{
Productcollection protites = new productcollection ();
Protites. Add (New Product {name = "A", site = "001 "});
Protites. Add (New Product {name = "B", site = "002 "});
Product expected = NULL;
Product actual = protites [0];
Assert. areequal (expected, actual );
}