简体   繁体   English

`public string this [string columnName]`当实现IDataErrorInfo接口时是什么意思?

[英]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]是什么意思public string this[string columnName]实现IDataErrorInfo接口的时候?

    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. 答:感谢Hans和Scott现在我知道这只是索引器的语法。 More information here . 更多信息在这里

That is a C# indexer , it lets you use your class like 这是一个C#索引器 ,它可以让你像使用类一样

IDataErrorInfo myClass = new MyClass();
string phoneNumberErrorInfo = myClass["PhoneNumber"];`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM