'is' operator
-
The MSDN on 'is' operator says ...the provided object can be cast to the provided type without causing an exception to be thrown. Suppose I have the following code:
object x = FunctionThatReturnsAnInteger();
if(x is decimal)
Console.WriteLine("Hello world");The above condition is never true because
x
is of type integer, not decimal, but - int can be cast to decimal without an exception being thrown - so why does it not work? More directly to my problem - what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal? I can obviously try this in a try-catch block, but I wonder if there are ways that perform better. Thanks for sugegstions, H. -
The MSDN on 'is' operator says ...the provided object can be cast to the provided type without causing an exception to be thrown. Suppose I have the following code:
object x = FunctionThatReturnsAnInteger();
if(x is decimal)
Console.WriteLine("Hello world");The above condition is never true because
x
is of type integer, not decimal, but - int can be cast to decimal without an exception being thrown - so why does it not work? More directly to my problem - what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal? I can obviously try this in a try-catch block, but I wonder if there are ways that perform better. Thanks for sugegstions, H.These are integral types, is only works for classes. This should not compile. The is operator WILL work with a class structure. class Base class Derived1 : Base Derived1 n; if ( n is Base ) { // this will work }
Member 1033907 wrote:
what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal?
I suspect you need your try/catch block. You also have the cost of boxing to contend with. You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
-
These are integral types, is only works for classes. This should not compile. The is operator WILL work with a class structure. class Base class Derived1 : Base Derived1 n; if ( n is Base ) { // this will work }
Member 1033907 wrote:
what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal?
I suspect you need your try/catch block. You also have the cost of boxing to contend with. You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
Thanks for your reply.
Christian Graus wrote:
This should not compile.
I compiles fine, 'is' works with structures as well. And besides - 'x' in the previous example is of type 'object', so the compiler has no way of knowing it is going to contain an integer.
Christian Graus wrote:
You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.
String parsing seems to me like the slowest possible approach. I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.
-
Thanks for your reply.
Christian Graus wrote:
This should not compile.
I compiles fine, 'is' works with structures as well. And besides - 'x' in the previous example is of type 'object', so the compiler has no way of knowing it is going to contain an integer.
Christian Graus wrote:
You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.
String parsing seems to me like the slowest possible approach. I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.
Member 1033907 wrote:
I compiles fine, 'is' works with structures as well.
I didn't think that was the case. I know I've had 'is' not compile for me before because I used an integral type.
Member 1033907 wrote:
String parsing seems to me like the slowest possible approach
Well, you're already paying the cost of boxing, which is expensive, and talking about try/catch, which is VERY expensive.
Member 1033907 wrote:
I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.
I would do some testing to see which is faster.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
-
Thanks for your reply.
Christian Graus wrote:
This should not compile.
I compiles fine, 'is' works with structures as well. And besides - 'x' in the previous example is of type 'object', so the compiler has no way of knowing it is going to contain an integer.
Christian Graus wrote:
You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.
String parsing seems to me like the slowest possible approach. I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.
I've got it. It's 'as' that doesn't work, because int cannot be null. But, is and as both rely on an inheritance tree, they do not try to do any sort of conversion between types. decimal does not derive from int, or vice versa, so neither of those will work. You're asking if the type can be converted, not if it is of that type to start with.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
-
The MSDN on 'is' operator says ...the provided object can be cast to the provided type without causing an exception to be thrown. Suppose I have the following code:
object x = FunctionThatReturnsAnInteger();
if(x is decimal)
Console.WriteLine("Hello world");The above condition is never true because
x
is of type integer, not decimal, but - int can be cast to decimal without an exception being thrown - so why does it not work? More directly to my problem - what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal? I can obviously try this in a try-catch block, but I wonder if there are ways that perform better. Thanks for sugegstions, H.Member 1033907 wrote:
int can be cast to decimal without an exception being thrown - so why does it not work?
Just because you can cast to it does not mean it is part of the same type heirarchy. Types can provide their own casting operators to make coercion to other types easier for the programmer.
is
/as
only work on things when they are along the same branch of an object heirarchy.decimal
derives fromobject
.int
derives fromobject
. Each is heading down different branches.Man who stand on hill with mouth open wait long time for roast duck to drop in
-
The MSDN on 'is' operator says ...the provided object can be cast to the provided type without causing an exception to be thrown. Suppose I have the following code:
object x = FunctionThatReturnsAnInteger();
if(x is decimal)
Console.WriteLine("Hello world");The above condition is never true because
x
is of type integer, not decimal, but - int can be cast to decimal without an exception being thrown - so why does it not work? More directly to my problem - what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal? I can obviously try this in a try-catch block, but I wonder if there are ways that perform better. Thanks for sugegstions, H.The term "cast" is used to mean two different things, one of which is more like "convert". When an
int
is "cast" todecimal
it gets converted. Theis
operator doesn't work with that type of cast.