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

  • SEARCH
  • Home
  • 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 5974699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:58:44+00:00 2026-05-22T20:58:44+00:00

Question 1> Should I check NULL in the following case? public interface INewClass {}

  • 0

Question 1> Should I check NULL in the following case?

public interface INewClass {}
public class NewClass : INewClass {}

public void FunMeA(INewClass obj)
{
    NewClass n = (NewClass) obj;
    ... // Should I check (n == null) here?
    // call methods defined inside INewClass (updated from NewClass to INewClass)
    ...
}

A concrete example,

public void FunMeB(IAsyncResult itfAR)
{
    AsyncResult ar = (AsyncResult) itfAR;
    ... // Should I check (ar == null) here?
    // access ar.AsyncDelegate
    ...
}

Question 2> I just start to transfer to C# from C++.
When coding in C++, I know when the checking should be done.
However, I am totally lost in the C# world. So the question is: is there
a general rule that can tell me when I MUST check for NULL?

Thank you

  • 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-05-22T20:58:44+00:00Added an answer on May 22, 2026 at 8:58 pm

    When doing this:

    NewClass n = (NewClass) obj;
    

    There is no point, because if it doesn’t cast it will throw an invalid cast exception.

    If you have any doubts as to whether or not you can actually cast it, you want to do:

    NewClass n = obj as NewClass;
    

    then

    if(n != null) ...
    

    The cast you are doing is called a direct cast, where the system will assume that it can be made. the n = obj as NewClass is called an indirect cast and is for those scenarios where you want to tell the program “Hey I think this will work, but if not don’t flip out and throw an exception…I’ll deal with it if it doesn’t work.”

    Using is vs as in casting.

    Depending on which scenario you want one will be better than the other. Technically from a performance perspective as is preferred. .Net will use atomically attempt to cast to the desired type and return null if it’s not, where with is it will have to walk the inheritance tree twice to see if it matches that type and then cast it. So in most cases if you want to see if you want to cast and use that type it’s better to:

    var obj = o as type
    if (obj != null)
    

    as opposed to

    if(o is type)
    {
       var obj = (type); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question is should i use properties for my view controllers? Consider the following case:
I'm using lucene in my project. Here is my question: should I use lucene
The question where exception logs should go has been discussed here once or twice
Another day , another question. My service layer has the following method public MatchViewData
In some library I create I have to use following cast: public void Foo(IList<uint>
Question: Should I write my application to directly access a database Image Repository or
This is a follow-up to the question: Should the folders in a solution match
[EDIT] Hmm. Perhaps this question should be titled what is the default user-input dialog
How can I resume a dehydrated orchestration ? the orchestration in question should have
I think I shall reframe my question from Where should you use BlockingQueue Implementations

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.