By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,767 Members | 1,605 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,767 IT Pros & Developers. It's quick & easy.

Does typeof or is work when dealing with an interface?

P: n/a
If I'm using DictionaryBase derived object lists and I iterate through them
with the IDictionary interface can I still check the object type by calling
typeof()?

btw I'm is using .NET 1.1

Thanks,
Jim

ex.
bool FindSpectialItem(string key, DictionaryBase[] MyDictionaryArray)
{
foreach(IDictionary dict in MyDictionaryArray)
{
bool found = false;
DerivedDictionaryA derivedDict = new DerivedDictionaryA ();

if(dict is DerivedDictionaryA) //or if(derivedDict.GetType() ==
typeof(DerivedDictionaryA))
{
//found a certain type
//look for key
found = true
}
else
continue;
}

return found;
}
Jan 30 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

Of course, typeof ALWAYS return the type of the reference
--
Ignacio Machin
machin AT laceupsolutions com
"Jim H" <ji**@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
| If I'm using DictionaryBase derived object lists and I iterate through
them
| with the IDictionary interface can I still check the object type by
calling
| typeof()?
|
| btw I'm is using .NET 1.1
|
| Thanks,
| Jim
|
| ex.
| bool FindSpectialItem(string key, DictionaryBase[] MyDictionaryArray)
| {
| foreach(IDictionary dict in MyDictionaryArray)
| {
| bool found = false;
| DerivedDictionaryA derivedDict = new DerivedDictionaryA ();
|
| if(dict is DerivedDictionaryA) //or if(derivedDict.GetType() ==
| typeof(DerivedDictionaryA))
| {
| //found a certain type
| //look for key
| found = true
| }
| else
| continue;
| }
|
| return found;
| }
|
|
Jan 30 '07 #2

P: n/a
Thanks!

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi,

Of course, typeof ALWAYS return the type of the reference
--
Ignacio Machin
machin AT laceupsolutions com
"Jim H" <ji**@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
| If I'm using DictionaryBase derived object lists and I iterate through
them
| with the IDictionary interface can I still check the object type by
calling
| typeof()?
|
| btw I'm is using .NET 1.1
|
| Thanks,
| Jim
|
| ex.
| bool FindSpectialItem(string key, DictionaryBase[] MyDictionaryArray)
| {
| foreach(IDictionary dict in MyDictionaryArray)
| {
| bool found = false;
| DerivedDictionaryA derivedDict = new DerivedDictionaryA ();
|
| if(dict is DerivedDictionaryA) //or if(derivedDict.GetType() ==
| typeof(DerivedDictionaryA))
| {
| //found a certain type
| //look for key
| found = true
| }
| else
| continue;
| }
|
| return found;
| }
|
|


Jan 30 '07 #3

P: n/a
Hello Jim,

Yes, you can use typeof to get Type instance from both class type or
interface, interface is just one of the .NET object types. Also, another
means to check whether an object is of a given type (in C#) is using the
"as" operator:

TypeName typedobj = UncastedObject as TypeName;

if(typedobj != null)
{

...........

}

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 31 '07 #4

P: n/a
On Jan 30, 12:45 pm, "Jim H" <j...@nospam.nospamwrote:
if(dict is DerivedDictionaryA) //or if(derivedDict.GetType() ==
typeof(DerivedDictionaryA))
Be aware, however, that the two tests do different things.

Assuming that DerivedDictionaryA is a class, then:

dict is DerivedDictionaryA

will be true if dict is a DerivedDictionaryA or any class derived from
DerivedDictionaryA.

dict.GetType() == typeof(DerivedDictionaryA)

will be true only if dict is a DerivedDictionaryA.

Jan 31 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.