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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:03:10+00:00 2026-05-16T15:03:10+00:00

I just ran into this situation and i thought it was a nice opportunity

  • 0

I just ran into this situation and i thought it was a nice opportunity to use the default keyword. But it doesn’t compile and i can’t think of why. The example below illustrates my problem:

public class Test<TDataSource>
{
    public IQueryable<TDataSource> DataSource { get; set; }

    public bool GetOneOrDefaultResult()
    {
        var result = DataSource.SingleOrDefault();
        return result != default(TDataSource);
    }
}

You’ll get an error on line 8 (“Operator ‘==’ cannot be applied to operands of type ‘TDataSource’ and ‘TDataSource’.”). I thought using the default keyword would eliminate any comparison problems between reference types and value types.

Adding a generic constraint limiting TDataSource to reference types makes this piece of code compile.

Can somebody explain why the compiler won’t fix this for me? Is it just not smart enough to see this would work?

This is related:
Can't operator == be applied to generic types in C#?

[Edit]
SLaks answer gave me some inspiration, the ‘==’ operator won’t work, but the Equals function should.

    public class Test<TDataSource>
{
    public IQueryable<TDataSource> DataSource { get; set; }

    public bool GetOneOrDefaultResult()
    {
        var result = DataSource.SingleOrDefault();
        return result.Equals(default(TDataSource));
    }
}

This compiles would this function correctly?

  • 1 1 Answer
  • 1 View
  • 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-16T15:03:11+00:00Added an answer on May 16, 2026 at 3:03 pm

    You cannot assume that every value type overrides the == operator. (And even if they did, there would be no way to call it using generics; it’s a static method)

    Instead, you should write

        return !(ReferenceEquals((object)result, (object)default(TDataSource)) 
              || result.Equals(default(TDataSource)));
    

    If result is null (and a reference type), the ReferenceEquals call will return true, so Equals won’t be called and won’t throw a NullReferenceException.
    If TDataSource is a value type, the ReferenceEquals will compare two different boxed references (which may happen to contain the same value, but will still be different), so it will pass on to the Equals call.

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

Sidebar

Related Questions

I just ran into a situation related to this SO question: How to tell
I am just learning how to use perl hashes and ran into this message
Just ran into this recently and I thought it'd be helpful to share. The
I just ran into this issue while making a GET request to a node.js
I'm new in python, and just ran into this statement data = dict( (k,
I am just getting started with NHaml and ran into a snag. This is
Ok I've just ran into this and I was only supposed to be checking
I just ran into this one and couldn't seem to get any clear answer
I'm trying to create a website for myself and just ran into this issue:
This is a very simplified version of some code I just ran into at

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.