typedef in c#
-
i need to understand what kind of object is just one.. i have myOwn it could be of Car or Bike. How can i understand it? i thought about: if( typedef(myOwn) == Car) but typedef doesn't exist in c# 1.1 I don't want to use try catch, tnx
your best bet is to use typeof() if(typeof(myOwn) == Car) DoSomethingWithCar(myOwn as Car) else DoSomethingWithBike(myOwn as Bike) The reason why a poor man will always be poor and the rich man will be rich, is because the poor always maximizes his expenditures and the rich man always maximizes his potential. --T.Parker
-
i need to understand what kind of object is just one.. i have myOwn it could be of Car or Bike. How can i understand it? i thought about: if( typedef(myOwn) == Car) but typedef doesn't exist in c# 1.1 I don't want to use try catch, tnx
-
if(myOwn is Car) { }
orif(myOwn.GetType() == typeof(Car)) { }
They are the same in essence.Just to add to
J4amieC
's suggestion, if you need to actually use the methods of your specific class, you could use theas
keyword to reduce the number of casts that you'll use. For example:Car c = someObject as Car;
if( c != null )
{
// Do Car-specific stuff in here.
}"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
Just to add to
J4amieC
's suggestion, if you need to actually use the methods of your specific class, you could use theas
keyword to reduce the number of casts that you'll use. For example:Car c = someObject as Car;
if( c != null )
{
// Do Car-specific stuff in here.
}"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
There's a small difference between using
is
andas
. If you useas
and then check fornull
you don't know ifsomeObject
actually is aBike
or if it's aCar
with valuenull
. Withis
you can really test for the correct type. Usually this won't matter, but it's a different meaning nevertheless... Regards, mav -
There's a small difference between using
is
andas
. If you useas
and then check fornull
you don't know ifsomeObject
actually is aBike
or if it's aCar
with valuenull
. Withis
you can really test for the correct type. Usually this won't matter, but it's a different meaning nevertheless... Regards, mavWell the idea is: Car car = someObj as Car; Boat boat = someObj as Boat; Plane plane = someObj as Plane; if (car != null) { } else if (boat != null) { } else if (plane != null) { } The assumption is that your testing classes on the same level in the class hierarchy. If your not testing classes on the same level, then the difference between
is
andas
matters. But you could also write your if-else statement so that it tests the more specific class types to the more broad class types.... lol Anyways, David -
There's a small difference between using
is
andas
. If you useas
and then check fornull
you don't know ifsomeObject
actually is aBike
or if it's aCar
with valuenull
. Withis
you can really test for the correct type. Usually this won't matter, but it's a different meaning nevertheless... Regards, mavI agree with you about most of that. If I use
typeof
on anull
value, then I still not get what I need in terms of runtime type information. Here, a use ofas
prevents an extra cast later on. For example, in the following code, we pass null values and with bothis
andas
, we cannot retrieve runtime type information:public class Foo {}
public class Goo {}
public class Test
{
[STAThread]
static void Main( string[] args )
{
Foo f = null;
Goo g = null;
Foo ff = new Foo();
Goo gg = new Goo();
Console.WriteLine( Form1.TestMeAsFoo( f ) ); // False
Console.WriteLine( Form1.TestMeAsFoo( g ) ); // False
Console.WriteLine( Form1.TestMeAsFoo( ff ) ); // True
Console.WriteLine( Form1.TestMeAsFoo( gg ) ); // False
Console.WriteLine( Form1.TestMeIsFoo( f ) ); // False
Console.WriteLine( Form1.TestMeIsFoo( g ) ); // False
Console.WriteLine( Form1.TestMeIsFoo( ff ) ); // True
Console.WriteLine( Form1.TestMeIsFoo( gg ) ); // False
}
public static bool TestMeIsFoo( object o )
{
return ( o is Foo );
}
public static bool TestMeAsFoo( object o )
{
return ( o as Foo ) != null;
}
}"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty -- modified at 15:24 Monday 5th December, 2005