【问题标题】:What is the meaning of `public string this[string columnName]` When implementing IDataErrorInfo interface?实现IDataErrorInfo接口时,`public string this[string columnName]`是什么意思?
【发布时间】:2016-07-15 20:32:30
【问题描述】:

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

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

我不明白为什么有方括号以及它的作用。


答案: 感谢 Hans 和 Scott,现在我知道这只是索引器的语法。更多信息here.

【问题讨论】:

  • 你不明白this[string columnName] 语法具体是什么,或者你在问他们为什么要这样做?
  • 在您最喜欢的 C# 语言书籍中查找“索引器”。
  • 你必须了解IDataErrorInfo,然后你就会明白This[columnName]的含义,我想这就是你被否决的原因。
  • @ScottChamberlain:语法。我不明白为什么会有方括号以及 id 的作用。
  • @HansPassant:谢谢,我去看看。

标签: c# wpf indexer idataerrorinfo


【解决方案1】:

这是一个C# indexer,它可以让你像使用你的类一样

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

【讨论】:

  • 对于给我投反对票的人,我的回答不正确或没有用怎么办?如果你不告诉我我做错了什么,我无法解决我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多