如何存储在C#中的哈希表中? [英] how store in hash table in c#?

查看:83
本文介绍了如何存储在C#中的哈希表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我想将数组存储在哈希表EX中:

 array [,] m =  array(){" 文件" 4 };
hashtable h =  hashtable();
h. add ( 1 ,m);

);

请帮助我错误:第1行中应使用嵌套的数组初始值设定项



 array [,] m =  array(){" 文件" 4 };  pre> 

解决方案

您的代码无法产生您显示的编译错误.错误将是找不到类型或名称空间数组"",因为不存在数组"这样的类型.第二行也不会编译,因为没有"hashtable"这样的类型.

显示错误消息和不相关的代码对您非常不利.如果您不提供任何有效的信息,您会期望得到什么帮助.

最接近的有效声明是

 对象 [] m =  对象 [] {"  13 . 4 }; 

但是,即使这样的数组在实践中通常也几乎没有意义.合法的.如果我知道您的目标,我可以解释该怎么办,但我对此并不希望.将数组放入哈希表似乎没有任何意义.即使使用非泛型哈希表也没有意义,因为此类类型被不需要进行不安全类型转换(在这种情况下,从System.Object到运行时类型)的泛型类型所取代.
您的问题表明,您对类型,初始化,数组,数组等级(为什么要声明二维数组并将其初始化为一维数组)一无所知.

—SA


我认为您的问题是错误的.
您没有问题将数据放入哈希表中……创建自动初始化的数组时遇到了问题.

如果您的数组是object [2,2],建议您显式初始化它:

 对象 [,] array =   2  0  0 ] = " 文档";
array [ 0  1 ] =  13  . 4 ; 



但是,然后,我认为您的数组不是object [,]我认为它只是对象.
因此,您的代码应如下所示:

 对象 [] array =  "  文档" 4 }; 



然后该数组将存在,您的其余代码将正确.


这里有一个示例供您参考

HashTable ht = new HashTable();
ht.Add("Firstname", "Eduard");
ht.Add("Lastname", "Lu");



其中第一个参数是键,第二个参数是值.

如果这解决了您的问题,请标记为已回答

最好的问候,
爱德华


Hi!
I want to store array in hash table EX:

array [,] m = new array(){"doucument",13.4};
hashtable h = new hashtable();
h.add(1,m );

);

please help me Error: A nested array initializer is expected in line 1



array [,] m = new array(){"doucument",13.4};

解决方案

Your code could not produce the compilation error you show. The error would be "The type or namespace ''array'' could not be found", because there is not such type as "array". Second line would not compile also, because there is no such type as "hashtable".

It is very bad of you to show an error message and unrelated code. What help could you expect if you don''t provide any valid information.

The closest valid declaration would be

object[] m = new object[] { "document", 13.4 };

But such arrays usually have little to no sense in practice even if they are legal. I could explain what to do if I knew your goals, but I don''t hope for that. Putting an array in hash table does not seem to make sense. Even using non-generic hash tables makes no sense as such types are superseded by generic types which do not require unsafe type casting (in this case, from System.Object to run-time types).

Your question shows that you have no idea of types, initializations, arrays, array ranks (why declaring two-dimensional array and initializing as single-dimentional?) and a lot more.

—SA


I think your question is wrong.
You are not having problems to put data into the hash... you are having problems creating an auto-initialized array.

If your array is object[2,2], I suggest you to initialize it explicity:

object[,] array = new object[2, 2];
array[0, 0] = "document";
array[0, 1] = 13.4;



But, then, I think your array is not object[,] I think it is only object.
So, your code should look like this:

object[] array = new object[]{"document", 13.4};



Then the array will exist and the rest of your code will be right.


here is a sample for your reference

HashTable ht = new HashTable();
ht.Add("Firstname", "Eduard");
ht.Add("Lastname", "Lu");



wherein the first parameter is the key and the second one is the value.

Please mark this as answered if this solved your problem

Best regards,
Eduard


这篇关于如何存储在C#中的哈希表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆