What is wrong with my code below or, alternatively, what is the point of a default indexer implementation if it isn't used by default?
public interface IFoo {
string this[string key] { get => "Default string index getter"; }
}
// Implementor
public class Foo : IFoo {}
// Usage
Foo myFoo = new();
var bar = myFoo["KEY"];
--> Cannot apply indexing to an expression of type 'Baz.Foo'
This is happening because you're referencing it by class, not by the interface.
It works fine if you use:
Why doesn't the class use the interface defaults, by default? Microsoft says:
Source: https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/interface-implementation/default-interface-methods-versions