From: cate on
.... ht = new Hashtable()
{
{"op1", (string)""},
{"op2", new Hashtable()}
}

When I access this hash, the compiler requires I cast it. Why?
Didn't I just define it?

This is required

string s = (string)ht["op1"];

where I would think the following would suffice. (it doesn't)

string s = ht["op1"];

Is this requirement to cast correct or do I have something else going
on? Thank you.
From: Alberto Poblacion on
"cate" <catebekensail(a)yahoo.com> wrote in message
news:a5008771-f10c-4d83-8b8e-11c5dab66b39(a)e2g2000yqn.googlegroups.com...
> ... ht = new Hashtable()
> {
> {"op1", (string)""},
> {"op2", new Hashtable()}
> }
>
> When I access this hash, the compiler requires I cast it. Why?
> Didn't I just define it?
>
> This is required
>
> string s = (string)ht["op1"];
>
> where I would think the following would suffice. (it doesn't)
>
> string s = ht["op1"];
>
> Is this requirement to cast correct or do I have something else going
> on? Thank you.

Yes, you do need the cast. ht["something"] returns an Object. At compile
time, it is not possible to know the type of the object, because at runtime
you can add or remove different things to the hashtable, so the compiler
can't know the type of the object stored there. If _you_ know the type, you
inform the compiler by means of the cast. Otherwise, the compiler doesn't
know whether there is a conversion to the target type. Since C# is a
strongly-typed language, requiring that all conversions be specified at
compile time, you have to write the conversion (cast) whenever the compiler
can't deduce it by itself.

From: Peter Duniho on
cate wrote:
> .... ht = new Hashtable()
> {
> {"op1", (string)""},
> {"op2", new Hashtable()}
> }
>
> When I access this hash, the compiler requires I cast it. Why?
> Didn't I just define it?
>
> This is required
>
> string s = (string)ht["op1"];
>
> where I would think the following would suffice. (it doesn't)
>
> string s = ht["op1"];
>
> Is this requirement to cast correct or do I have something else going
> on? Thank you.

As Alberto points out, yes�for that type (System.Collections.Hashtable),
the cast is required.

Note, however, that there is (since .NET 2.0) generics and the
System.Collections.Generic namespace. In there, you will find the
Dictionary<TKey, TValue> class, with which you can declare the actual
types used by the hashtable dictionary, which in turn allows the
dictionary members to be strongly typed, avoiding the need for casting.

Pete
From: Arne Vajhøj on
On 10-05-2010 08:19, cate wrote:
> ... ht = new Hashtable()
> {
> {"op1", (string)""},
> {"op2", new Hashtable()}
> }
>
> When I access this hash, the compiler requires I cast it. Why?
> Didn't I just define it?
>
> This is required
>
> string s = (string)ht["op1"];
>
> where I would think the following would suffice. (it doesn't)
>
> string s = ht["op1"];
>
> Is this requirement to cast correct or do I have something else going
> on?

C# is a relative strong typed language. The compiler tries
to check the assignment. When assigning an object to a string
an explicit cast is necessary, because by doing that you tell
the compile "I know this is a string". If you lied to the compiler,
then you get an exception at runtime.

The string cast on "" is completely unnecessary though. It does
not provide any value at all.

Arne
From: Arne Vajhøj on
On 10-05-2010 11:37, Peter Duniho wrote:
> cate wrote:
>> .... ht = new Hashtable()
>> {
>> {"op1", (string)""},
>> {"op2", new Hashtable()}
>> }
>>
>> When I access this hash, the compiler requires I cast it. Why?
>> Didn't I just define it?
>>
>> This is required
>>
>> string s = (string)ht["op1"];
>>
>> where I would think the following would suffice. (it doesn't)
>>
>> string s = ht["op1"];
>>
>> Is this requirement to cast correct or do I have something else going
>> on? Thank you.
>
> As Alberto points out, yes�for that type (System.Collections.Hashtable),
> the cast is required.
>
> Note, however, that there is (since .NET 2.0) generics and the
> System.Collections.Generic namespace. In there, you will find the
> Dictionary<TKey, TValue> class, with which you can declare the actual
> types used by the hashtable dictionary, which in turn allows the
> dictionary members to be strongly typed, avoiding the need for casting.

It would not work in this case, because the type of the two values
are different.

Arne