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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:23:15+00:00 2026-06-03T17:23:15+00:00

The following [c#] code will not compile with the error operator == cannot be

  • 0

The following [c#] code will not compile with the error “operator == cannot be applied to operands of type ‘T’ and ‘T'”.

public class Widget<T> where T: IComparable
{
    public T value;
    public Widget(T input) { value = input; }
    public bool Equals<T>(Widget<T> w) where T : System.IComparable
    {
        return (w.value == value);
    }
}

Is there a way to constrain the type T of the w input parameter to be the same type T as the object being compared thus guaranteeing they can be compared against each other and eliminating the compiler error? Using (dynamic) in front of value as below allows it to compile but it seemed like there would be a better way that would catch an issue at compile time:

public bool Equals<T>(Widget<T> w) where T : System.IComparable
{
return (w.value == (dynamic) value);
}

  • 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-06-03T17:23:18+00:00Added an answer on June 3, 2026 at 5:23 pm

    Assuming you’re really interested in value equality here, you can just use EqualityComparer.Default:

    EqualityComparer<T> comparer = EqualityComparer<T>.Default;
    return comparer.Equals(w.value, value);
    

    Note that currently your Equals method is generic too, trying to declare another type parameter T. I don’t think you really want to do that.

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

Sidebar

Related Questions

The following code does not compile: public class GenericsTest { public static void main(String[]
The following code using boost::asio will not compile: #ifndef _SERVER_H_ #define _SERVER_H_ #include Connection.h
Please consider the following code and comments: Console.WriteLine(1 / 0); // will not compile,
The following code will not compile with G++ 4.5 or 4.6 (snapshot). It will
The following code does not compile: class OuterClass<T> { class BaseClass { } class
The following code will not compile against the Async CTP in Visual Studio 2010:
The following code will add the categories selector widget to the WordPress Page editor
The following source file will not compile with the MSVC compiler (v15.00.30729.01): /* stest.c
The following code fails to compile on g++ 4.6.1: template<class Base> struct GetBase {
Let's see the following code snippet in Java. public class Main { public static

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.