Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8921313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:30:31+00:00 2026-06-15T06:30:31+00:00

What is a good usage of the is-operator? The construct below for casting is

  • 0

What is a good usage of the is-operator?

The construct below for casting is not the recommended way to go, virtually all documentation prefers the as-operator with a null-check.

if(obj is SomeClass)
{
  SomeClass some = (SomeClass)obj;
  ....
}

And sure this is a (very small) performance increase and some even mention the tread safety.
And yes this is true…

So, why do we have the is-operator?
Where does the “as-operator with a null-check” not work or is not the way to go?
Does is have an advantage to restrict the scope of you declaration you get by using the is-operator?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:30:32+00:00Added an answer on June 15, 2026 at 6:30 am

    as doesn’t work with non-nullable structs:

    object o = 123;
    int i = o as int; // compile error
    

    however:

    object o = 123;
    if(o is int) {
        int i = (int)o;
        //...
    }
    

    of course, from 2.0 onwards you could also use:

    int? i = o as int?;
    

    and test for null like usual.

    There is also the scenario that you don’t care about the values of the object… you just need to know what it is:

    if(obj is Something)
        throw new InvalidOperationException("Seriously, don't do that");
    // phew! dodged a bullet; we're ok here...
    

    Note that GetType() is not appropriate for this, as you don’t want to have to consider subclasses, interfaces, etc manually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a good way to track CPU and memory usage for a
I've seen it used, but I'm not sure the usage were good usecase examples.
What is a good site to check current web usage statistics -- particularly Java
I've gone through and made sure the memory usage is good on my app
Good day. I have little usage of Flash CS4, however i have to build
Good afternoon, I have a quick 'top level' question regarding the usage of Facebook
I am a C# guy with pretty good grasp of Visual Studio IDE usage
Are there any good, free tools to profile memory usage in C# ? Details:
I've found some good examples of functors on SO like this one, and all
Are their any good websites (characterized by high usage), that use JSF for their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.