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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:47:30+00:00 2026-05-24T20:47:30+00:00

Why is ThrowIfNull implemented as: static void ThrowIfNull<T>(this T argument, string name) where T

  • 0

Why is ThrowIfNull implemented as:

    static void ThrowIfNull<T>(this T argument, string name) where T : class
    {
        if (argument == null)
        {
            throw new ArgumentNullException(name);
        }
    }

Wouldn’t it be better rewritten as:

    static void ThrowIfNull<T>(this T argument, string name) where T : class
    {
        if (object.ReferenceEquals(argument, null))
        {
            throw new ArgumentNullException(name);
        }
    }

Pros: it helps avoiding confusing Equals overloads and probably makes the code more clear.

Any cons to that? There should be some.

  • 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-24T20:47:31+00:00Added an answer on May 24, 2026 at 8:47 pm

    There’s no difference between the two. You’re confusing overriding Equals (which isn’t called in either implementation) with overloading == (which won’t be relevant in either snippet as overloading is performed at compile time, and the compiler doesn’t know enough about T to use any specific overload).

    Just to show what I mean:

    static void ThrowIfFoo<T>(this T argument, string name) where T : class
    {
        if (argument == "foo")
        {
            throw new Exception("You passed in foo!");
        }
    }
    

    Testing with:

    "foo".ThrowIfFoo(); // Throws
    
    string x = "f";
    x += "oo"; // Ensure it's actually a different reference
    
    x.ThrowIfFoo(); // Doesn't throw
    

    ThrowIfFoo doesn’t know that T will be a string – because that depends on the calling code – and the overload resolution is only performed when ThrowIfFoo is compiled. Therefore it’s using the operator ==(object, object) rather than ==(string, string).

    In other words, it’s like this:

    object foo1 = "foo";
    
    string tmp = "f";
    object foo2 = tmp + "oo";
    
    Console.WriteLine(foo1.Equals(foo2)); // Prints True
    Console.WriteLine(foo1 == foo2); // Prints false
    Console.WriteLine((string) foo1 == (string) foo2); // Prints True
    

    In the last line, the compiler knows it can use the overload of == because both operands have compile-time types of string.

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

Sidebar

Related Questions

Hi I coded this OneAtRandom() extension method: public static class GenericIListExtensions { public static
I've a generic class that helps me to do checks on argument values: internal
I was curious to see how the SingleOrFallback method was implemented in MoreLinq and
I've written a custom LINQ extension method that extends the TakeWhile() method to be
I've written an extension method for use with WCF services that keeps all the
I've just written a couple of pagination extension methods and I was curious to

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.