Looking for best practices, suggestions for ' is ' and ' as ' keywords

  • Thread starter Thread starter ChrisMiddle10
  • Start date Start date
C

ChrisMiddle10

Are there serious to moderate performance issues to weigh when deciding
to use the 'is' and 'as' keywords over - let's say - more explicit
checks for null and type? Any best practices or heuristics for their
usage?

Snippet 1:

if ( obj == null && obj.GetType( ) == typeof( MyType ) )
{
MyType myType = ( MyType ) obj;
}

Snippet 2:

if ( obj is MyType )
{
MyType myType = obj as MyType;
}

Obviously, Snippet 2 is much cleaner, which I like, but am I losing
anything?


Thanks!

- Chris
 
Hello (e-mail address removed),

Try this http://ekrishnakumar.wordpress.com/2006/07/08/c-is-and-as-operators/

Are there serious to moderate performance issues to weigh when
deciding to use the 'is' and 'as' keywords over - let's say - more
explicit checks for null and type? Any best practices or heuristics
for their usage?

Snippet 1:

if ( obj == null && obj.GetType( ) == typeof( MyType ) )
{
MyType myType = ( MyType ) obj;
}
Snippet 2:

if ( obj is MyType )
{
MyType myType = obj as MyType;
}
Obviously, Snippet 2 is much cleaner, which I like, but am I losing
anything?

Thanks!

- Chris
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
You use "as" when you are not sure of the type. For example, if a method can
return any of several types, it may not return the same type as the type
expected. Example:

public object foo(int i)
{
if (i == 0) return new TypeA();
if (i == 1) return new TypeB();
return new TyeC();
}

int i = someFunction();
TypeC foo = (TypeC) foo(i);

Since the function can return any of several different types, casting it as
TypeC will throw an exception if the type is not TypeC. Instead, you would
use:

TypeC foo = foo(variableName) as TypeC;

In this case, if the type is not TypeC, foo is set to null.

The use of "is" is much easier to understand, and I think you do understand
it.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 
You gained an extra type conversion (Snippet 2 does it twice) and you lost
the check for type equality.

Snippet 3:

MyType myType = obj as MyType;
if ( myType != null )
{
// you end up here if obj is a MyType object OR if obj's type is derived
from MyType.
}
 
* (e-mail address removed) wrote, On 19-7-2006 18:40:
Are there serious to moderate performance issues to weigh when deciding
to use the 'is' and 'as' keywords over - let's say - more explicit
checks for null and type? Any best practices or heuristics for their
usage?

Snippet 1:

if ( obj == null && obj.GetType( ) == typeof( MyType ) )
{
MyType myType = ( MyType ) obj;
}

Snippet 2:

if ( obj is MyType )
{
MyType myType = obj as MyType;
}

Obviously, Snippet 2 is much cleaner, which I like, but am I losing
anything?

Even better would be:


MyType myType = obj as MyType;
if (myType != null)
{
// Do something with it.
}

As you're no longer tackling the fact that obj might be null. Also doing
an 'is' check and then an 'as' will perform the type check twice instead
of once.

Alternatively (if the null check is not required), this is the fastest way:

if ( obj is MyType )
{
MyType myType = (MyType) obj;
}

Should you require a null check anyways, the first option is better.

Jesse
 
Jesse Houwing said:
Even better would be:


MyType myType = obj as MyType;
if (myType != null)
{
// Do something with it.
}

As you're no longer tackling the fact that obj might be null. Also doing
an 'is' check and then an 'as' will perform the type check twice instead
of once.

Alternatively (if the null check is not required), this is the fastest way:

if ( obj is MyType )
{
MyType myType = (MyType) obj;
}

Should you require a null check anyways, the first option is better.

If you don't care about whether it's null or not, the first option is
faster. It only requires examining the type once rather than twice. The
difference is very slight though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top