I have a DICOM dictionary that contains a set of objects all deriving from DataElement. The dictionary has an int as a key, and the DataElement as property. My DICOM dictionary contains a this[] property where I can access the DataElement, like this:
public class DicomDictionary
{
Dictionary<int, DataElement> myElements = new Dictionary<int, DataElement>();
.
.
public DataElement this[int DataElementTag]
{
get
{
return myElements[int];
}
}
}
A problem now is that I have different DataElement types all deriving from DataElement, like DataElementSQ, DataElementOB and so on. What I wanted to do now is the following to make writing in C# a little bit easier:
public T this<T>[int DataElementTag] where T : DataElement
{
get
{
return myElements[int];
}
}
But this is not really possible. Is there something I have missed? Of course I could do it with Getter method, but it would be much nicer to have it this way.
The best options are to either use a generic method (instead of an indexer), or to have your class be generic (in which case, the indexer would be tied to the class generic type). A generic indexer as you've described is not allowed in C#.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With