C# 阵列结构

C# 阵列结构,c#,arrays,structure,C#,Arrays,Structure,我想分配哈希表到数组的结构,请帮助下面是代码 public void SendCmdWithData(int IntCode, Hashtable Htbl) { DatawithCommandCode[] arrData = new DatawithCommandCode[Htbl.Count]; try { for (int intVal = 0; intVal < Htbl.Count - 1; intVal

我想分配哈希表到数组的结构,请帮助下面是代码

    public void SendCmdWithData(int IntCode, Hashtable Htbl) 
    {
        DatawithCommandCode[] arrData = new DatawithCommandCode[Htbl.Count];

        try {
            for (int intVal = 0; intVal < Htbl.Count - 1; intVal++) {
            }
        }
        catch (Exception) {}
    }

您可以通过以下方式实现:

 public static void SendCmdWithData(int IntCode, Hashtable Htbl)
        {
            DatawithCommandCode[] arrData = new DatawithCommandCode[Htbl.Count];
            try
            {
                int i = 0;
                foreach (System.Collections.DictionaryEntry element in Htbl)
                {
                    DatawithCommandCode tempData = new DatawithCommandCode();
                    tempData.CmdCode = Convert.ToInt32(element.Key);
                    tempData.Value = Convert.ToInt32(element.Value);
                    arrData[i++] = tempData;
                }
            }
            catch (Exception) { }
        }

但我希望您使用的是.Net framework 2.0或更高版本,最好使用

您是否尝试自己分配它?您遇到了哪些错误或异常?@Azodious:我不知道哈希表是键值,结构是结构类型,如果可能的话,请避免使用
哈希表
,而是使用通用
字典
。非常好,这就是我一直在寻求的帮助,非常感谢
 public static void SendCmdWithData(int IntCode, Hashtable Htbl)
        {
            DatawithCommandCode[] arrData = new DatawithCommandCode[Htbl.Count];
            try
            {
                int i = 0;
                foreach (System.Collections.DictionaryEntry element in Htbl)
                {
                    DatawithCommandCode tempData = new DatawithCommandCode();
                    tempData.CmdCode = Convert.ToInt32(element.Key);
                    tempData.Value = Convert.ToInt32(element.Value);
                    arrData[i++] = tempData;
                }
            }
            catch (Exception) { }
        }