Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

c# - What is the generic version of a Hashtable?

I have been learning the basics of generics in .NET. However, I don't see the generic equivalent of Hashtable. Please share some sample C# code for creating generic hashtable classes.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Dictionary<TKey, TValue>

Note that Dictionary is not a 100% drop in replacement for HashTable.

There is a slight difference in the way they handle NULLs. The dictionary will throw an exception if you try to reference a key that doesn't exist. The HashTable will just return null. The reason is that the value might be a value type, which cannot be null. In a Hashtable the value was always Object, so returning null was at least possible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...