Store values in Hashtable

Asked

Viewed 109 times

5

When I keep the value of this Hastable he makes me wrong.

Follows the code:

Hashtable ht = new Hashtable();
ht.Add("index", index);
ht.Add("tipo", "1");

string tipo = ht["tipo"];

2 answers

6


When you re-turn a Hastable value you have to convert it to String like this:

string tipo = ht["tipo"].ToString();

6

First, do not use this data structure, it is considered obsolete and should no longer be used by any application. Prefer Dictionary<K, V>. Once this is done the problem will not occur. If you insist you will have performance problems and errors of this type, which can even be solved (making conversion, for example), but not worth the effort.

var ht = new Dictionary<string, string> { ["index"] = "0", ["tipo"] = "1" };
string tipo = ht["tipo"];

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Thank you, but the exercise was imperative Hastable :)

  • Thank you, but the exercise was imperative Hastable :)

  • Jump out of the exercise. Thing that asks to do wrong thing will only bring you harm.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.