private List<string> _S3 = new List<string>();
public string S3[int index]
{
get
{
return _S3[index];
}
}
唯一的问题是我得到13个错误。我想调用string temp = S3[0];
并从列表中获取具有特定索引的字符串值。使用属性从列表中获取价值<string>
private List<string> _S3 = new List<string>();
public string S3[int index]
{
get
{
return _S3[index];
}
}
唯一的问题是我得到13个错误。我想调用string temp = S3[0];
并从列表中获取具有特定索引的字符串值。使用属性从列表中获取价值<string>
你不能这样做,在C#中的 - 你不能命名一样,在C#索引。你可以要么有一个命名的属性,没有参数,或你可以有一个索引器的参数,但没有名字。
当然,你可以有使用一个名称,其回报与索引值的属性。例如,对于一个只读视图,您可以使用:
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>
每个访问S3
,但这似乎有点毫无意义。
试试这个
private List<string> _S3 = new List<string>();
public List<string> S3
{
get
{
return _S3;
}
}
暴露一个ICollection的
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];
不,'_S3'是*私人*。创建属性的要点是从超出范围的地方访问存储在_S3中的值。 – Otiel
好点。我忽略了问题中的私人修饰语。 然后你想创建一个像LarsTech建议的公共方法。 – jmshapland
什么是错误 –