# [.net] what's a good use for the HashTable?

This topic is 4483 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So i'm messing around with the HashTable and there is not method to get an object by its key. so what good is this table for if you can't retrieve anything directly from it? what a typical use for a hash-table in C#?

##### Share on other sites
er what?

I don't have an IDE or my usual links in front of me, but HashTable[Key] should produce the object from its key. HashTable is [a bit simplistically] a .NET 1.1 Dictionary, without the benefits that Generics provide.

##### Share on other sites
You can get associated values, you just haven't looked at the documentation well enough.

Much like a C++ std::tr1::unordered_map, you have an indexing operator[], so you can get the value associated with a particular key by doing:

Hashtable table = new Hashtable();
table[12] = "Dude";
string val = (string)table[12];
if( val != null )
System.Console.WriteLine( val );

You can also check for a key with Table.ConatinsKey().

If you're using .NET 2.0, it might be better to use System.Collections.Generic.Dictionary<> since that would avoid the boxing/unboxing.

jfl.

##### Share on other sites
thanks for the examples guys. i kinda decided to abandon the documentation because it doesnt really seem to help much. first of all it seems slow.

but here's a question...

public static void PrintKeysAndValues( Hashtable myHT )  {      Console.WriteLine( "\t-KEY-\t-VALUE-" );      foreach ( DictionaryEntry de in myHT )         Console.WriteLine("\t{0}:\t{1}", de.Key, de.Value);      Console.WriteLine();   }

..that's a C# example function that prints the contents of a HashTable.
Now why do "they" print using the argument {0} thing all the time?

ie. Console.WriteLine("\t{0}:\t{1}", de.Key, de.Value);

This seems like a waste of time, and possibly even make it less readable, than just a straight-up WriteLine with no argument list thing.

##### Share on other sites
You'd prefer?
Console.Write("\t");Console.Write(de.Key);Console.Write(":\t");Console.Write(de.Value);Console.WriteLine("");

I don't have an ide to check if the params version of Write will just dump parameters, but I doubt it. Personally, I find the format version more readable; it keeps the actual message together so that you don't have to read around the syntax.

Also, format strings like that can be easily formed so that it's quite simple to pass a differing output format depending on a variable. It's quite hard to re-arrange Write commands in code based on variables.

##### Share on other sites
im reading this "intro to C#" book and it has Write examples like this:

int age = 20;
Console.Write("Age: {0}", age);

or

string name = "Speilberg";
Console.WriteLine("Name: {0}", name);

##### Share on other sites
Both Console.Write and Console.WriteLine have the ability to take objects/strings as parameters for both the straight output and the formatted solution. Another option you have is:
Console.WriteLine("Hello, " + name + ". You are " + age + " years old!");

It's really up to you which one you use. I personally prefer the following as it allows you to easily change things around:
Console.WriteLine("Hello, {0}. You are {1} years old!", name, age);

##### Share on other sites
Quote:
 Original post by Rob LoachBoth Console.Write and Console.WriteLine have the ability to take objects/strings as parameters for both the straight output and the formatted solution. Another option you have is:Console.WriteLine("Hello, " + name + ". You are " + age + " years old!");It's really up to you which one you use. I personally prefer the following as it allows you to easily change things around:Console.WriteLine("Hello, {0}. You are {1} years old!", name, age);

It also allows you to specify formatting information along with the argument - for example:
int Value = 1234;Console.WriteLine("{0} in hex is {1:X4}", Value, Value);

Less typing is always good. [grin]

##### Share on other sites
Quote:
 Original post by GekkoCubethanks for the examples guys. i kinda decided to abandon the documentation because it doesnt really seem to help much. first of all it seems slow.

What do you mean? The documentation would have answered your question if you'd read it. So how can you say it doesn't help much, if you don't read it? (In fact, MSDN is a pretty damn good and comprehensive source of documentation. I haven't seen much that beats it)
As for it being "slow", what's that got to do with anything? Surely, it's faster than posting a question here and waiting for a reply? [wink]

##### Share on other sites
Quote:
Original post by benryves
Quote:
 Original post by Rob LoachBoth Console.Write and Console.WriteLine have the ability to take objects/strings as parameters for both the straight output and the formatted solution. Another option you have is:Console.WriteLine("Hello, " + name + ". You are " + age + " years old!");It's really up to you which one you use. I personally prefer the following as it allows you to easily change things around:Console.WriteLine("Hello, {0}. You are {1} years old!", name, age);

It also allows you to specify formatting information along with the argument - for example:
int Value = 1234;Console.WriteLine("{0} in hex is {1:X4}", Value, Value);

Less typing is always good. [grin]

The most interesting side-effect of using the formatted output, which doesn't seem to have been mentionned yet, is that it is much easier to localize your applications using this method. Well, scratch that. It's actually impossible to localize your applications using Console.Write("Hello my name is " + entity.Name + " and I'm " + entity.Age + "years old."); for any given output with actual complexity.

Think of us foreigners. [wink]

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
18
5. 5

• 9
• 9
• 33
• 13
• 13
• ### Forum Statistics

• Total Topics
632584
• Total Posts
3007218

×