x


How to compare typeof to a dictionary

I have a dictionary< string, object >. The object values can either be a string or another dictionary. If it's a string, it will be used to move through a switch statement. If it is a dictionary, an if statement that checks for the dictionary type will cause an early return before the switch and essentially load said dictionary as the current dictionary.

When my game was JavaScript, I could check if( myDictionary.TypeOf() == Dictionary.< string, object > ){...}

But in C# that syntax doesn't work (and yeah, I changed the Dictionary<T,T> syntax omitting the period.)

I tried if( myDictionary is Dictionary< string, object > ){...}

I can see that the variable is of the right type via the console, but it's not satisfying the conditional.

So... my question is-- how can I check for the type of the variable that may contain a dictionary... or is there some other strategy I should be considering?

more ▼

asked Nov 06 '11 at 10:43 AM

Sinaz20 gravatar image

Sinaz20
1 10 10 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

With the typeof operator you get the type-object for a certain class / type.

if( myDictionary.TypeOf() == typeof(Dictionary< string, object >) )

not tested but should work ;)

more ▼

answered Nov 06 '11 at 11:45 AM

Bunny83 gravatar image

Bunny83
46.9k 12 50 210

Thanks, that worked out.

Nov 06 '11 at 07:21 PM Sinaz20
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4374
x57
x53

asked: Nov 06 '11 at 10:43 AM

Seen: 1228 times

Last Updated: Nov 06 '11 at 07:21 PM