private List<string> _S3 = new List<string>();
public string S3[int index]
{
get
{
return _S3[index];
}
}
唯一的问题是我得到了13个错误。我想调用string temp = S3[0];
并从列表中获取特定索引处的字符串值。
private List<string> _S3 = new List<string>();
public string S3[int index]
{
get
{
return _S3[index];
}
}
唯一的问题是我得到了13个错误。我想调用string temp = S3[0];
并从列表中获取特定索引处的字符串值。
private readonly List<string> _S3 = new List<string>();
// You'll need to initialize this in your constructor, as
// _S3View = new ReadOnlyCollection<string>(_S3);
private readonly ReadOnlyCollection<string> _S3View;
// TODO: Document that this is read-only, and the circumstances under
// which the underlying collection will change
public IList<string> S3
{
get { return _S3View; }
}
这样,从公共角度来看,底层集合仍然是只读的,但您可以使用以下方式访问元素:
string name = foo.S3[10];
ReadOnlyCollection<string>
,但这似乎有点无意义。C#的属性不能有参数。 (顺便说一句:VB.Net可以。)
您可以尝试使用函数代替:
public string GetS3Value(int index) {
return _S3[index];
}
public class Foo
{
public int this[int index]
{
get
{
return 0;
}
set
{
// use index and value to set the value somewhere.
}
}
}
_S3[i] 应该自动返回位置 i 上的字符串
所以只需要这样做:
string temp = _S3[0];
试试这个
private List<string> _S3 = new List<string>();
public List<string> S3
{
get
{
return _S3;
}
}
我会选择
class S3: List<string>{}
_S3
是私有的。创建属性的目的是从超出范围的地方访问存储在_S3
中的值。 - Otiel