Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 693
Saeid Avatar asked Mar 28 '16 14:03

Saeid


1 Answers

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

IDataErrorInfo myClass = new MyClass();
string phoneNumberErrorInfo = myClass["PhoneNumber"];`
like image 136
Scott Chamberlain Avatar answered Nov 03 '22 00:11

Scott Chamberlain