9

What is the meaning of public string this[string columnName] When implementing IDataErrorInfo interface?

    public string this[string columnName]
    {
        get
        {
            switch (columnName)
            {
                case "Name":
                    return ValidateName();
                case "PhoneNumber":
                    return ValidatePhoneNumber();
                default:
                    return string.Empty;
            }
        }
    }

I don't get it why there are square parentheses and what it does.


Answer: Thanks to Hans and Scott now I know that is simply the syntax of an indexer. More information here.

Saeid
  • 691
  • 7
  • 26

1 Answers1

22

That is a C# indexer, it lets you use your class like

IDataErrorInfo myClass = new MyClass();
string phoneNumberErrorInfo = myClass["PhoneNumber"];`
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • 7
    To the person who downvoted me, what about my answer was incorrect or not useful? I can't fix my question if you don't tell me what I have done wrong. – Scott Chamberlain Mar 28 '16 at 15:12