[Mono-list] Hashtable behaviour

Carlo Kok ck at carlo-kok.com
Tue Sep 5 10:50:02 EDT 2006


Glen Ford wrote:
> 
> Has else anyone noticed the following on mono 1.1.16.1 ...
> 
> Hashtable table = new Hashtable();
> int myvalue = (int)table[32];
> 
> Generates an exception:
> 
> Unhandled Exception: System.NullReferenceException: Object reference not
> set to an instance of an object
> 
> 
> However the following doesn't ...
> 
> Hashtable table = new Hashtable();
> string myvalue = (string)table["blah"];
> 
> 
> I would have expected a NullReferenceException in the second case as well.

Same happens on .NET. First one is an unbox operation and unboxing
requires an instance (it's a valuetype). Second is a cast operation.
((string)null) is perfectly valid.

--
Carlo Kok


More information about the Mono-list mailing list