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?
Using explicit implementation of interfaces, you can do something like:
so implement two different enumerators with the same signature (where signature = type/number of input parameters). Note that you can have multiple interfaces, each one with an indexer with a different return type, so given x interfaces and one class, you can have x + 1 distinct indexers with the same signature.
Now... If
Student
was a "real" class instead of anobject
, you could do some tricks with implicit casts (you can't do implicit casts with/against theobject
class):(the indexer returns a
MyObject
class that then can be implicitly casted toint
/string
)then use as
Note that I don't think this is a good idea. But you asked for it.
As noted by Spoi1ler, the return type is not part of the method signature. So you can't do it like shown in your code.
You can provide multiple indexers using an explicit interface as demonstrated in Xanatos' answer.
The code doesn't make any sense though. Just use one indexer and store and return the student object, so you can do
string name = lstStudent[0].Name;
.