如何在C#.net中分配值时分配数组的键

How can i assign a key of a array on the time of assigning a value in C#.net

我是ASP.NET的新手,我想在数据库中插入动态表单中的数据。为了标识字段,我还保存了数据库中字段的标签。

为此,我想使用多维数组。问题是,我想在分配值时声明数组的键,就像下面在PHP中所做的那样:

1
myarray['key_label'] = 'key_value';

我不知道如何在C.NET中实现这一点。为此,我搜索并找到了字典,所以我使用了以下代码,但在这种情况下,我可以一次查看任何键或值:

1
2
3
4
 var dictionary = new Dictionary<string, object>();
 dictionary.Add("username", rpu);
 dictionary.Add("sec_username", rcu);
 dictionary.Add("co_type", ct);

请引导我到下面

  • 如何分配上面提到的数组的值?

  • 我找到了一种同时显示键和值的方法,如下所述,但是我是否可以像处理数组那样使字典具有多维性?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    Dictionary<int, string> plants = new Dictionary<int, string>() {    
    {1,"Speckled Alder"},    
    {2,"Apple of Sodom"},    
    {3,"Hairy Bittercress"},    
    {4,"Pennsylvania Blackberry"},    
    {5,"Apple of Sodom"},    
    {6,"Water Birch"},    
    {7,"Meadow Cabbage"},    
    {8,"Water Birch"}    
    };

    Response.Write("dictionary elements........<br />");

    //loop dictionary all elements  
    foreach (KeyValuePair<int, string> pair in plants)
    {
        Response.Write(pair.Key +"....." + pair.Value +"<br />");
    }

    //find dictionary duplicate values.  
    var duplicateValues = plants.GroupBy(x => x.Value).Where(x => x.Count() > 1);

    Response.Write("<br />dictionary duplicate values..........<br />");

    //loop dictionary duplicate values only            
    foreach (var item in duplicateValues)
    {
        Response.Write(item.Key +"<br />");
    }

    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
   Dictionary<string,object> dic = new Dictionary<string,object>(){
            {"key1","value1
<div class="
suo-content">[collapse title=""]<ul><li>谢谢,Ashkan有没有办法让它多维度的</li><li>@我编辑了我的答案看看</li><li>谢谢@askhan脱帽致谢!让我试试,然后再给你答复。</li></ul>[/collapse]</div><hr>
<blockquote>
  <p>
how can I assign the value of an array like I mentioned above?
</p>
</blockquote>

<p>
Yes, you can access a dictionary entry in C# like you would an array in PHP. Something like <wyn>myDictionary["
keyOne"] = varOne;</wyn>
</p>

<blockquote>
  <p>
I found a way to display both key and values together, mentioned below
  but is it possible that i can make dictionary multi dimensional like
  we do for arrays?
</p>
</blockquote>

<p>
Yes, like in PHP where you make an array of arrays, you would in C# create a dictionary of dictionaries:
</p>

[cc lang="
csharp"]Dictionary<string,Dictionary<string,object>> multiDict = new Dictionary<string,Dictionary<string,object>>();
multiDict["
dictionary1"]["keyOne"] = varOne;