27
A
回答
52
在C#中,你得到的最接近的是Dictionary<TKey, TValue>
:
var dict = new Dictionary<string, string>();
dict["key_name"] = "value1";
注意,一個Dictionary<TKey, TValue>
是不一樣PHP的關聯數組,因爲它是只有訪問一個類型的密鑰(TKey
- 在上例中爲string
),而不是字符串/整數鍵的組合(感謝Pavel澄清這一點)。
這就是說,我從來沒有聽說過.NET開發人員抱怨過這個。
在回答您的評論:
// The number of elements in headersSplit will be the number of ':' characters
// in line + 1.
string[] headersSplit = line.Split(':');
string hname = headersSplit[0];
// If you are getting an IndexOutOfRangeException here, it is because your
// headersSplit array has only one element. This tells me that line does not
// contain a ':' character.
string hvalue = headersSplit[1];
2
嘗試字典:
var dictionary = new Dictionary<string, string>();
dictionary.Add("key_name", "value1");
4
你可以使用一個Dictionary<TKey, TValue>
:
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["key_name"] = "value1";
5
嗯,我猜你想要一本字典:
using System.Collections.Generic;
// ...
var dict = new Dictionary<string, string>();
dict["key_name1"] = "value1";
dict["key_name2"] = "value2";
string aValue = dict["key_name1"];
0
您還可以使用KeyedCollection http://msdn.microsoft.com/en-us/library/ms132438%28v=vs.110%29.aspx,其中您的值是複雜類型並且具有唯一屬性。
你必須從KeyedCollection,例如您的收藏繼承...
public class BlendStates : KeyedCollection<string, BlendState>
{
...
這需要您覆蓋GetKeyForItem方法。
protected override string GetKeyForItem(BlendState item)
{
return item.DebugName;
}
然後,在這個例子中,集合由字符串(BlendState的調試名)索引:
OutputMerger.BlendState = BlendStates["Transparent"];
0
因爲其他人說的字典,我決定用2個數組回答。 一個數組將成爲另一個數組的索引。
您沒有真正指定在特定索引處可以找到的數據類型,所以我繼續爲我的示例選擇字符串。
您也沒有指定是否希望能夠在稍後調整大小。如果你這樣做,你可以使用List<T>
而不是T []
,其中T是類型,如果需要,只需公開某些公共方法以添加每個列表。
這裏是你如何做到這一點。這也可以被修改以將可能的索引傳遞給構造函數,或者使其成爲可能,但是您可以這樣做。
class StringIndexable
{
//you could also have a constructor pass this in if you want.
public readonly string[] possibleIndexes = { "index1", "index2","index3" };
private string[] rowValues;
public StringIndexable()
{
rowValues = new string[ColumnTitles.Length];
}
/// <summary>
/// Will Throw an IndexOutofRange Exception if you mispell one of the above column titles
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public string this [string index]
{
get { return getOurItem(index); }
set { setOurItem(index, value); }
}
private string getOurItem(string index)
{
return rowValues[possibleIndexes.ToList().IndexOf(index.ToLower())];
}
private void setOurItem(string index, string value)
{
rowValues[possibleIndexes.ToList().IndexOf(index.ToLower())] = value;
}
}
你會然後調用它像這樣:
StringIndexable YourVar = new YourVar();
YourVar["index1"] = "stuff";
string myvar = YourVar["index1"];
相關問題
- 1. 將字符串數組設置爲ImageView
- 2. 將字符串轉換爲int數組
- 3. 將int轉換爲字符串數組
- 4. 將字符串轉換爲int數組
- 5. php parse_ini_file將鍵設置爲字符串
- 6. 將字符串拆分爲數組並將分隔符設置爲鍵
- 7. 將(Int,字符串)轉換爲字符串以打印數組
- 8. 將字符串中的字符設置爲Int?
- 9. 將字符串數組轉換爲int(月份爲數字)
- 10. 是否將變量設置爲字符串,字符串文字?
- 11. 如何將字符串拆分爲int數組中的int
- 12. 使用linq2sql將Concat DateTime和int字段設置爲字符串
- 13. 未設置數組的鍵如果key爲字符串
- 14. 在C中將字符串數組轉換爲Int數組#
- 15. 試圖將字符串數組轉換爲Int數組的JSP
- 16. 將字符串數組轉換爲int數組''System.FormatException'
- 17. 將一個字符串數組轉換爲一個Int數組
- 18. 如何將字符串數組轉換爲int數組?
- 19. 如何將int數組更改爲一個字符串數組?
- 20. 將int數組轉換爲java中的字符串數組
- 21. 重構Ruby:將字符串數組轉換爲int數組
- 22. 將字符串數組轉換爲int數組android
- 23. C#將字符串數組轉換爲Int數組
- 24. C#參數不能將int轉換爲字符串或字符串int
- 25. 將字符串轉換爲int,int轉換爲字符串
- 26. 轉換int數組爲字符串JAVA
- 27. 轉換字符串數組爲int
- 28. 字符串到int數組
- 29. Laravel:如何將主鍵和外鍵設置爲字符串
- 30. 無法將SQL參數值設置爲字符串數組
你確定通過索引訪問關聯數組的定義的一部分?見http://en.wikipedia.org/wiki/Associative_array – Odrade 2010-07-12 18:58:47
@Odrade:我根本沒有;)這就是爲什麼我編輯我的答案,特別提到「PHP的關聯數組」(可能在你的評論之後)。 – 2010-07-12 18:59:32
@Odrade:在PHP數組的情況下,它們不是「按索引」訪問的 - 相反,整數可以是字符串旁邊的鍵(並且存在非常複雜的轉換方案,例如「8」和「8」 「'是同樣的鑰匙,但'」08「'不是)。從某種意義上說,它們不是索引,它們不能識別陣列中元素的位置。 – 2010-07-12 19:08:21