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 808853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:37:54+00:00 2026-05-15T00:37:54+00:00

I was looking through the Domain Oriented N-Layered .NET 4.0 Sample App project and

  • 0

I was looking through the “Domain Oriented N-Layered .NET 4.0 Sample App” project and ran across some code that I do not understand. In this project they often use syntax like the following to check arguments for null:

public GenericRepository(IQueryableContext context,ITraceManager traceManager)
{
    if (context == (IQueryableContext)null)
            throw new ArgumentNullException("context", Resources.Messages.exception_ContainerCannotBeNull);

Why would you cast null to the type of the object you are checking for null?

  • 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-15T00:37:55+00:00Added an answer on May 15, 2026 at 12:37 am

    It’s pointless in the example given.

    While not applicable in this case, there is sometimes a need to cast null (or at least there was before default(T) was added. Consider the following:

    void DoSomething(string x) {
        ...
    }
    
    void DoSomething(object x) {
        ...
    }
    
    DoSomething(null);            // compiler can't infer the type
    DoSomething((string)null);    // string type is now explicit
    DoSomething(default(string)); // same as previous
    

    EDIT

    Just thought of another case where you would have to do the cast when testing equality. If you had an object that had an overloaded == operator that allowed comparison with two reference types, comparing against null would be ambiguous. However because IQueryableContext is most likely an interface and interfaces cannot overload the == operator, I still don’t see any valid reason to do it in the example you gave.

    class CustomObject {
    
        private string _id;
    
        public CustomObject(string id) {
            _id=id;
        }
    
        public static bool operator ==(CustomObject lhs, CustomObject rhs) {
            if (ReferenceEquals(lhs, rhs)) { return true; }
            if (ReferenceEquals(lhs, null)) { return false; }
            if (ReferenceEquals(rhs, null)) { return false; }
            return lhs._id == rhs._id;
        }
    
        public static bool operator !=(CustomObject lhs, CustomObject rhs) {
            return !(lhs == rhs);
        }
    
        public static bool operator ==(CustomObject lhs, string rhs) {
            if (ReferenceEquals(lhs, rhs)) { return true; }
            if (ReferenceEquals(lhs, null)) { return false; }
            if (ReferenceEquals(rhs, null)) { return false; }
            return lhs._id == rhs;
        }
    
        public static bool operator !=(CustomObject lhs, string rhs) {
            return !(lhs==rhs);
        }
    
    }
    
    CustomObject o = null;
    if (o == null) {
        Console.WriteLine("I don't compile.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.