【发布时间】:2013-04-02 00:54:33
【问题描述】:
我想实现一个属性,该属性根据它接收到的索引返回一个值。我不是只是封装了一个私有数组。事实上,我将返回的数据没有存储在任何数组中,而是存储在成员对象中。这个数组属性只是一种以索引方式访问这些数据的方式,而无需以索引方式存储它。
根据this 文章,以下应该可以工作:
public double Angles[int i]
{
get { // return a value based on i; }
}
但是,我收到以下错误:
The type or namespace 'i' could not be found (are you missing a using directive or an assembly reference?)
Invalid token ']' in class, struct or interface member declaration
Invalid expression term 'int'
Bad array declarator: To declarate a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
从这些错误中,我认为编译器似乎认为我正在尝试创建一个数组成员。显然我的语法在这里是错误的。谁能告诉我这样做的正确方法?
【问题讨论】:
-
在 C# 中搜索索引器
标签: c# arrays properties