【问题标题】:Non-integer indexed Indexer properties in C#C# 中的非整数索引索引器属性
【发布时间】:2012-10-10 17:51:57
【问题描述】:

我想在 C# 中有一个索引属性:

public Boolean IsSelected[Guid personGuid]
{
   get {
      Person person = GetPersonByGuid(personGuid);
      return person.IsSelected;
   }
   set {
      Person person = GetPersonByGuid(personGuid);
      person.IsSelected = value;
   }
}
public Boolean IsApproved[Guid personGuid]
{
   get {
      Person person = GetPersonByGuid(personGuid);
      return person.IsApproved;
   }
   set {
      Person person = GetPersonByGuid(personGuid);
      person.IsApproved= value;
   }
}

Visual Studio 抱怨非整数索引器语法:

我知道.NET supports non-Integer indexors


我会用另一种语言写:

private
   function GetIsSelected(ApproverGUID: TGUID): Boolean;
   procedure SetIsSelected(ApproverGUID: TGUID; Value: Boolean);
   function GetIsApproved(ApproverGUID: TGUID): Boolean;
   procedure SetIsApproved(ApproverGUID: TGUID; Value: Boolean);
public
   property IsSelected[ApproverGuid: TGUID]:Boolean read GetIsSelected write SetIsSelected;
   property IsApproved[ApproverGuid: TGUID]:Boolean read GetIsApproved write SetIsApproved;
end;

【问题讨论】:

  • C# does not limit the index type to integer. - 来自here。不知道为什么会抱怨。
  • 您收到的实际错误是什么?
  • 您在 [] 之前错过了 this 关键字
  • @MohsenAfshin 我没有错过this,我只是称它为IsSelected。如果它被称为this,那么这意味着只有一个索引属性。

标签: c# properties indexed-properties


【解决方案1】:

你的语法不正确:

public Boolean this[Guid personGuid]
{
   get {
      Person person = GetPersonByGuid(personGuid);
      return person.IsSelected;
   }
   set {
      Person person = GetPersonByGuid(personGuid);
      person.IsSelected = value;
   }
}

索引器是使用 this 关键字声明的 - 您不能使用自己的名称。

来自Using Indexers (C# Programming Guide)

要在类或结构上声明索引器,请使用 this 关键字


此外,只能有一个接受类型的索引器 - 这是 C# 索引器语法的限制(可能是 IL 限制,不确定)。

【讨论】:

  • 这意味着我不能有两个(例如IsSelected[Guid personGuid]IsApproved[Guid personGuid])索引属性?
  • @IanBoyd - 是的。索引器的限制之一。虽然你可以有一个IsApproved[string reallyAGUID] 并解析成一个 GUID(丑陋的 hack,但你知道我要去哪里)。
  • @IanBoyd:正确。如果它们具有不同类型的键,您可以拥有多个索引器,但您不能让索引按您希望的方式工作。最好使用 ToggleSelected(Guid personGuid)ToggleApproval(Guid personGuid) 方法或类似的方法。
  • "不能做 - 索引器的限制" 更新你的答案,我会接受的。
【解决方案2】:

索引器仅适用于 this 关键字。见here

this 关键字用于定义索引器。

【讨论】:

  • @AnthonyPegram:我已经被证明错误的次数足够多,因此在做出绝对陈述时要小心。
  • 我认为编译器有办法关闭整个系统。
【解决方案3】:

就像 Matt Burland 和 Oded 说的,索引器只使用这个关键字,所以你需要一个代理类,它有你需要的接口:

public class PersonSelector
{
    private MyClass owner;

    public PersonSelector(MyClass owner)
    {
        this.owner = owner;
    }

    public bool this[Guid personGuid]
    {
       get {
          Person person = owner.GetPersonByGuid(personGuid);
          return person.IsSelected;
       }
       set {
          Person person = owner.GetPersonByGuid(personGuid);
          person.IsSelected = value;
       }
    }

}

public class MyClass
{
    public MyClass()
    {
        this.IsSelected = new PersonSelector(this);
    }   

    public PersonSelector IsSelected { get; private set; }

    ...
}

【讨论】:

    【解决方案4】:

    @Jojan 回复here:

    C# 3.0 规范

    “索引器的重载允许类、结构或接口 声明多个索引器,前提是它们的签名在其中唯一 那个类、结构或接口。”

    或者如果您的数据集很小,您可以使用 IList

    public IList<Boolean> IsSelected
    {
       get { ... }
    }
    
    public IList<Boolean> IsApproved
    {
       get { .... }
    }
    

    或使用Multiple Indexers technique using Interfaces

    【讨论】:

      【解决方案5】:

      实际上你可以有多个索引器接受类型。

      但是,您不能有两个具有相同签名的索引器。相同的签名意味着参数编号和类型 - 所以上面的代码有两个具有相同签名的索引器。

      如果代码改为:

          Boolean this[string x, Guid personguid]
      

      和:

          Boolean this[Guid personguid]
      

      它应该可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-16
        • 1970-01-01
        • 2016-07-10
        • 1970-01-01
        • 2010-11-08
        • 1970-01-01
        • 2017-02-07
        • 1970-01-01
        相关资源
        最近更新 更多