2010-07-12 23 views

回答

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]; 
+0

你確定通過索引訪問關聯數組的定義的一部分?見http://en.wikipedia.org/wiki/Associative_array – Odrade 2010-07-12 18:58:47

+0

@Odrade:我根本沒有;)這就是爲什麼我編輯我的答案,特別提到「PHP的關聯數組」(可能在你的評論之後)。 – 2010-07-12 18:59:32

+0

@Odrade:在PHP數組的情況下,它們不是「按索引」訪問的 - 相反,整數可以是字符串旁邊的鍵(並且存在非常複雜的轉換方案,例如「8」和「8」 「'是同樣的鑰匙,但'」08「'不是)。從某種意義上說,它們不是索引,它們不能識別陣列中元素的位置。 – 2010-07-12 19:08:21

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"]; 
相關問題