Using Indexer Definitions in C# Interfaces

Interface with Indexer:

public interface IStringDef
{
  string this[int index] { get; set; }
}

Class implementing the interface:

class MyClass : IStringDef
{
  private List<string> allStrings = new List<string>();
 
  public string this[int index]
  {
    get { return allStrings[index]; }
    set { allStrings.Insert(index, value); }
  }
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.