Comparing Types

  • Thread starter Thread starter derek
  • Start date Start date
D

derek

newbie question:

if I have a Type object that is the type of a particular instance, how
can
I compare it to given Types to determine what type it is.

i want to be able to branch depending on the type of an instance.
if Type is String
if Type is String[]
if Type is Collection
etc
 
You should be able to use typeof, like

if (myType is typeof(string))
{
}
else
....

Bennie Haelen
 
derek said:
if I have a Type object that is the type of a particular
instance, how can I compare it to given Types to determine
what type it is.

if (myType == typeof(string)) // or whichever type you want

P.
 
Not "is", you want to use "==" there.
"is" will get the type of an instance in the LHS and compare it to a type in
the RHS.

Bennie Haelen said:
You should be able to use typeof, like

if (myType is typeof(string))
{
}
else
...

Bennie Haelen
newbie question:

if I have a Type object that is the type of a particular instance, how
can
I compare it to given Types to determine what type it is.

i want to be able to branch depending on the type of an instance.
if Type is String
if Type is String[]
if Type is Collection
etc
 
John said:
Not "is", you want to use "==" there.
"is" will get the type of an instance in the LHS and compare it to a type in
the RHS.

You should be able to use typeof, like

if (myType is typeof(string))
{
}
else
...

Bennie Haelen
newbie question:

if I have a Type object that is the type of a particular instance, how
can
I compare it to given Types to determine what type it is.

i want to be able to branch depending on the type of an instance.
if Type is String
if Type is String[]
if Type is Collection
etc
you are absolutely correct, did not have my coffee yet I guess ;-)

Bennie
 
Back
Top