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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:46:42+00:00 2026-05-10T23:46:42+00:00

I have some generic types, like the following: public struct Tuple<T1, T2> { …

  • 0

I have some generic types, like the following:

public struct Tuple<T1, T2> { ... } public struct Tuple<T1, T2, T3> { ... } etc. 

These should in theory be able to compare themselves against other values of the same type, so that I can write the following type of code:

List<Tuple<Type, String>> l = new List<Tuple<Type, String>>(); l.Add(new Tuple<Type, String>(typeof(ISomeInterface), '123')); if (l.Contains(new Tuple<Type, String>(typeof(ISomeOtherInterface), '123'))     ... 

unfortunately, there was a bug in my code, and the question then becomes how to do this correctly.

The bug had to do with my implementation of CompareTo>, which basically looks like this:

Int32 result = HelperMethods.CompareTwoFields<T1>(_Value1, other._Value1); if (result != 0)     return result;  Int32 result = HelperMethods.CompareTwoFields<T2>(_Value2, other._Value2); if (result != 0)     return result;  return 0; 

HelperMethods.CompareTwoFields looks like this:

internal static Int32 CompareTwoFields<T>(T field1, T field2) {     Int32 result = 0;     if (ReferenceEquals(field1, null) != ReferenceEquals(field2, null))         result = ReferenceEquals(field1, null) ? -1 : +1;     else if (!ReferenceEquals(field1, null) && field1 is IComparable<T>)         result = ((IComparable<T>)field1).CompareTo(field2);     else if (!typeof(T).IsValueType)     {         if (Object.ReferenceEquals(field1, field2))             return 0;         else             return field1.ToString().CompareTo(field2.ToString());     }     return result; } 

The last if-statement there is something I tucked on now to fix the bug, but is this correct?

Basically, how do I compare two Type objects? Is there a meaningful comparison for these, except just to convert them to a string and compare?

  • 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. 2026-05-10T23:46:42+00:00Added an answer on May 10, 2026 at 11:46 pm

    Do you mean:

    bool equal = EqualityComparer<T>.Default.Equals(val1,val2); 

    This works with IEquatable<T> (for T : IEquatable<T>), else falls back to object.Equals.

    There is also Comparer<T>.Default for inequality comparisons (greater/less):

    int delta = Comparer<T>.Default.Compare(val1,val2); 

    This uses T : IComparable<T>, or T : IComparable otherwise.

    By the way, Type should just use the regular reference compare provided by object.Equals, so it should just work fine with the above.

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

Sidebar

Ask A Question

Stats

  • Questions 266k
  • Answers 266k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can apply this approach http://byatool.com/mvc/custom-data-annotations-with-mvc-how-to-check-multiple-properties-at-one-time/ To include the ID… May 13, 2026 at 12:36 pm
  • Editorial Team
    Editorial Team added an answer You can get the remote peer's address using wf_platform:get_peername/0. @spec… May 13, 2026 at 12:36 pm
  • Editorial Team
    Editorial Team added an answer I think your view is getting dumped by the didReceiveMemoryWarning… May 13, 2026 at 12:36 pm

Related Questions

I have some generic types, like the following: public struct Tuple<T1, T2> { ...
Update: See the bottom of this question for a C# workaround. Hi there, Consider
I am dealing with a set of native functions that return data through dynamically-allocated
I have problems to get generics to work in the following scenario: Delphi provides
I'm working on a little Java game in which all sorts of events can

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.