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

Related Questions

I have some classes layed out like this class A { public virtual void
I'm writing some Enum functionality, and have the following: public static T ConvertStringToEnumValue<T>(string valueToConvert,
Here's the situation: We have some generic graphics code that we use for one
I'm trying to build my first generic list and have run into some problems.
I have to work on some code that's using generic lists to store a
Imagine the following simple code: public void F<T>(IList<T> values) where T : struct {
I have a couple of generic lists with completely different types of data. In
I have some objects like user, address and so on, and Im converting them
I have the following code: import java.util.*; public class SellTransaction extends Transaction { private
I have some UI in VB 2005 that looks great in XP Style, but

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.