Search code examples
c#indexertype-parameter

Is there any way to use a type-parameter within an indexer declaration?


I don't think so, but before I give up and use a method:

public T this<T>[String index]
{
    get{
        //return stuff
    }
}

I don't have a type parameter in the class. I just want to return a different type based on what I want to get.

for example:

myObject<String>["SavedString"]

Is there a syntax for that? (I can't compile any of the code above, of course.)


Solution

  • No, properties can't be generic in C#. Nor can events, constructors, variables, operators or finalizers.

    Basically there are generic types and generic methods, and that's it.