Question

I want an Indexer but types of the setter and the getter should be different.

public Foo this[string key]
{
    get
    {
        return FindFoo(key);
    }
}
public Bar this[string key]
{
    set
    {
        DoMagic(key, value);
    }
}

When I try:

myClass["hello"] = coolBar;

It says: The call is ambiguous

Is there anyway to do this?

Was it helpful?

Solution

I want an Indexer but types of the setter and the getter should be different.

You can't do that, basically - not when the two indexers have the same signature.

Even if you could, I'd say it would be a pretty odd design, which would surprise users of your API.

The relevant bit of the C# specification is section 10.9:

The signature of an indexer must differ from the signatures of all other indexers declared in the same class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top