I'd like to add different indexer implementations in my class :
SpecificCollection
public class SpecificCollection<T> : ISpecificCollection <T>
{
public int this[int index]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public object this[int index]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public string this[int index]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public event void OnAddElement;
public event void OnRemoveElement;
public void AddNewElement(T element)
{
throw new NotImplementedException();
}
public void DeleteElement(int index)
{
throw new NotImplementedException();
}
public int Count
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
StudentSpecificCollection
public class StudentSpecificCollection : SpecificCollection<Student>
{
private string[] arrName;
private int[] arrAge;
private object[] arrStudent ;
public int ISpecificCollectionIndexers<Student>.this[int index]
{
get
{
return arrAge[index];
}
set
{
arrAge[index] = value;
}
}
object ISpecificCollectionIndexers<Student>.this[int index]
{
get
{
return arrStudent[index];
}
set
{
arrStudent[index] = value;
}
}
string ISpecificCollectionIndexers<Student>.this[int index]
{
get
{
return arrName[index];
}
set
{
arrName[index] = value;
}
}
public event void OnAddElement;
public event void OnRemoveElement;
public void AddNewElement(Student element)
{
object objStudent = arrStudent.Where(x => x != null).LastOrDefault();
int index = (objStudent == null) ? 0 : Array.IndexOf(arrStudent, objStudent);
arrName[index] = element.Name ;
arrAge[index] = element.Age;
arrStudent[index] = element;
}
public void DeleteElement(int index)
{
if (index > Count - 1) return;
arrName[index] = null;
arrAge[index] = -1;
arrStudent[index] = null;
}
public int Count
{
get
{
return arrName.Where(x=>x !=null).Count();
}
set
{
}
}
public StudentSpecificCollection()
{
arrName = new string[100];
arrAge = new int[100];
arrStudent = new object[100];
}
}
So I need to know :
- How can I use the different indexer implementation?
- What are the best practises to implement differents kinds of indexation in this class?
- In which cases customizing indexation were better than using the different C# collections?