[英]Get a list of JSON property names from a class to use in a query string
[英]Use Property to get value from List<string>
private List<string> _S3 = new List<string>();
public string S3[int index]
{
get
{
return _S3[index];
}
}
唯一的問題是我得到13個錯誤。 我想調用string temp = S3[0];
並從具有特定索引的列表中獲取字符串值。
你不能在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];
您可以在每次訪問S3
創建一個新的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>{}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.