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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:51:17+00:00 2026-05-22T22:51:17+00:00

I have read the following statement regarding to the comparison of C# value types

  • 0

I have read the following statement regarding to the comparison of C# value types
in C# in Depth, Second Edition several times.

page 77,

When a type parameter is unconstrained (no constraints are applied to it), you can use == and != operators, but only to compare a value of that type with null. You can’t compare two values of type T with each other.

…

When a type parameter is constrained to be a value type, == and != can’t be used with it at all.

If I understand (I don’t think so) it correctly, it basically tells me that you cannot
use == or != to compare two value types. Why why why?

It will be better if a simple example can be given for this case. Can someone give me
a little idea what the above paragraph tries to convey?

  • 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-22T22:51:18+00:00Added an answer on May 22, 2026 at 10:51 pm

    It simply means this when constraining to a value type (second paragraph)

    static bool TryToCompare<T>(T first, T second) where T : struct
    {
        return first == second; // not legal
        return first.Equals(second); // legal
    }
    

    Without the value-type constraint on the generic, it also says this (first paragraph)

    static bool TryToCompare<T>(T first, T second) 
    {
        return first == second; // not legal
        return first == null; // legal
        return first.Equals(second); // legal
    }
    

    If you constrain T to a reference type, you can get away with using ==

    static bool TryToCompare<T>(T first, T second) where T : class
    {
        return first == second; // legal
        return first == null; // legal
        return first.Equals(second); // legal
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following output sing var_dump.. How do I read the value of
I have read the following statement. The status argument given to _exit() defines the
Hello I have read following php statement from a blog but I am unable
First of all let me tell you that i have read the following questions
So I have the following read and write file program for an Array. How
i executed the following c code on my ubuntu machine...i have read about fcntl()'s
I am having following issue with reading binary file in C. I have read
I have to read the xml node name from the following XML, but I
Have a look at the following code you are not required to read the
I have the following code: $SQL = UPDATE jobs SET read = '1' WHERE

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.