c# - 方法声明中的方括号如何适合 c#?

发布于 2024-09-19 09:04:34 字数 330 浏览 8 评论 0原文

方法声明中的方括号如何适合 C#?这是我在阅读 WPF 验证时看到的,可以使用 IDataErrorInfo 和以下示例。

public string this[string propertyName]

// Error handling takes place here.
public string this[string propertyName]  // <== IE HERE
{
  get
  // etc 
  }
}

我注意到 MSDN 说“方括号 ([]) 用于数组、索引器和属性。它们也可以与指针一起使用。”那么上面的用法是指针吗?

How do square brackets in a method declaration fit in with c#? That is I see when reading up on WPF validation one can use IDataErrorInfo with an example of the following.

public string this[string propertyName]

// Error handling takes place here.
public string this[string propertyName]  // <== IE HERE
{
  get
  // etc 
  }
}

I note MSDN says "Square brackets ([]) are used for arrays, indexers, and attributes. They can also be used with pointers." So is the above usage a pointer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏别ゝ 2024-09-26 09:05:09

那将是一个索引器属性。它们对于自定义集合很有用:

public class MyCustomCollection
{
    List<MyObject> _list = new List<MyObject>();

    public string this[string name]
    {
        get { return _list.Single(o => o.Name == name)
                          .Select(o => o.Description);
    }

    public string this[int id]
    {
        get { return _list.Single(o => o.Id == id).Select(o => o.Description);
    }
}

然后您可以使用该集合,例如:

MyCollection col = new MyCollection();

// Fill the collection

string description = col["Name"];
string description2 = col[2];

That would be an indexer property. They're useful on custom collections:

public class MyCustomCollection
{
    List<MyObject> _list = new List<MyObject>();

    public string this[string name]
    {
        get { return _list.Single(o => o.Name == name)
                          .Select(o => o.Description);
    }

    public string this[int id]
    {
        get { return _list.Single(o => o.Id == id).Select(o => o.Description);
    }
}

And then you can use the collection like:

MyCollection col = new MyCollection();

// Fill the collection

string description = col["Name"];
string description2 = col[2];
天冷不及心凉 2024-09-26 09:05:01

这是索引器的声明。它类似于数组索引。 propertyName 是一个字符串,该方法使用它来索引某种集合。该方法从集合中返回相应的字符串。

当然,该方法可以做其他事情,但这就是语义的含义。

This is a declaration of an Indexer. It's analagous to array indexing. propertyName is a string which the method uses to index into some kind of collection. The method returns the corresponding string from the collection.

Of course, the method could do something else, but that's what the semantics mean.

树深时见影 2024-09-26 09:04:54

这是 C# 语言的一项标准功能,称为索引器。通常,您在编写自己的集合或类似类型时会使用它们。这是一个简短的(非现实世界)示例。

public class Foo {
    private List<int> m_Numbers = new List<int>();

    public int this[int index] {
        get {
            return m_Numbers[index];
        }
        set {
            m_Numbers[index] = value;
        }
    }
}

class Program {
    static void Main() {
        Foo foo = new Foo();
        foo[0] = 1;
    }
}

如果您有创造力,您可以使用索引器做很多很酷的事情,这是该语言的一个非常巧妙的功能。

This is a standard feature of the C# language called an Indexer. Generally you would use these when writing your own collections, or similar types. Here is a brief (not real world) example.

public class Foo {
    private List<int> m_Numbers = new List<int>();

    public int this[int index] {
        get {
            return m_Numbers[index];
        }
        set {
            m_Numbers[index] = value;
        }
    }
}

class Program {
    static void Main() {
        Foo foo = new Foo();
        foo[0] = 1;
    }
}

There's a lot of cool things you can use indexers for if you are creative, it's a really neat feature of the language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文