Search This Blog

Tuesday, August 13, 2013

Indexer in C#.net

Indexer in c#, Indexers allow instances of a class or struct to be indexed just like arrays. for creating indexer we need to inherit System.Collection.CollectionBase.

example-

public class EmployeeCollection:System.Collection.CollectionBase
{
        //adding class to indexer
        public void Add(Employee obEmployee)
        {
                this.InnerList.aAdd(obEmployee);
        }
        //get the data
        public Employee this[int i]
        {
                get
                {
                        return
                                {
                                        this.InnerList[i] as Employee;
                                }
                }
        }
}

for add data into collection create object of collection and call add method. For get data collection[index].

No comments:

Post a Comment