【发布时间】: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