如何聲明索引屬性?什麼是在C#索引一個屬性的正確語法
public class PublishProperties : ScriptableObject {
List<string> m_shellPathsT = new List<string>();
List<string> m_shellPathsL = new List<string>();
public List<string> ShellPath[int index]
{
get
{
if (index == 0)
return m_shellPathsT;
else
return m_shellPathsL;
}
}
這不會編譯,我不知道如何編碼。由於其他要求,我必須擁有兩個不同的列表,這些列表都是這樣聲明的。
我通常會具有一個列表的數組...
或類似這樣的
public List<string>[] m_shellPaths = { new List<string>(), new List<string>() };
然而,這又沒有其他因素的工作......(基本上有一些系列化出現這種情況自動不與構造函數或像上述聲明的變量工作)。
我說得沒錯,你是否試圖爲自己的班級創建索引訪問? – bash0r 2013-02-19 23:35:35
http://msdn.microsoft.com/en-us/library/vstudio/6x16t2tx.aspx你需要將屬性更改爲公共字符串this [int i] – 2013-02-19 23:36:50
你只能創建一個索引器(比如'string this [int index ]'),你不能建立索引屬性。爲了達到這個目的,只需將'ShellPath'的類型實現爲'this [int]' – millimoose 2013-02-19 23:36:52