Generics and Indexers

G

Guest

trying to create a generic indexer where the class definition does not have a
<T>
psedo code below.



public myClass{
public T this<T>[ValueType item]{
get{
return null;
}
}
}



other than creating two methods for get and set are there any other
solutions? or explinations as to why this would not work


Any help would be very much appriciated
 
M

Marc Gravell

Not without creating some kind of (invented) Indexer<T> class to act as
an intermediary; this would add code to the caller (e.g.
obj.GetIndexer<float>()[123]=39F) such that it is probably easier to
just to forget about an indexer, e.g. obj.SetValue(123,39F) or
SetValue<float>(123,39F) [depending on whether T-inference applies -
i.e. GetValue would always need the T, SetValue probably not as it can
infer from the param)...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top