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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:46:19+00:00 2026-05-27T17:46:19+00:00

I’ve been following a post regarding extension methods from this post : public static

  • 0

I’ve been following a post regarding extension methods from this post:

public static IEnumerable<T> Distinct<T,TKey>(this IEnumerable<T> list, Func<T,TKey> lookup) where TKey : struct {
        return list.Distinct(new StructEqualityComparer<T, TKey>(lookup));
    }

class StructEqualityComparer<T,TKey> : IEqualityComparer<T> where TKey : struct {

    Func<T, TKey> lookup;

    public StructEqualityComparer(Func<T, TKey> lookup) {
        this.lookup = lookup;
    }

    public bool Equals(T x, T y) {
        return lookup(x).Equals(lookup(y));
    }

    public int GetHashCode(T obj) {
        return lookup(obj).GetHashCode();
    }
}

Could someone explain the purpose of the where TKey : struct appended to the extension method and comparator class. Removing these statements seems to make no difference to a simple test code – both evaluations TKey are of type int on a class and struct respectively:

public struct TestMeStruct
{
    public int a;
    public int b;
}

public class TestMeClass
{
    public int a { get; set; }
    public int b { get; set; }

}

public void Test()
{

        List<TestMeStruct> lstruct = new List<TestMeStruct>();

        lstruct.Add(new TestMeStruct() { a = 1, b = 2 });
        lstruct.Add(new TestMeStruct() { a = 3, b = 7 });
        lstruct.Add(new TestMeStruct() { a = 3, b = 14 });
        lstruct.Add(new TestMeStruct() { a = 32, b = 11 });


        List<TestMeClass> lclass = new List<TestMeClass>();
        lclass.Add(new TestMeClass() { a = 1, b = 2 });
        lclass.Add(new TestMeClass() { a = 3, b = 7 });
        lclass.Add(new TestMeClass() { a = 3, b = 14 });
        lclass.Add(new TestMeClass() { a = 32, b = 11 });

        var one = lstruct.Distinct(mem => mem.a).ToList();
        var two = lclass.Distinct(mem => mem.a).ToList();
 }

Both return identical lists. Much obliged for any clarity on what’s going on!

  • 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-27T17:46:19+00:00Added an answer on May 27, 2026 at 5:46 pm

    I believe you know what is generic type constraints and especially where : struct, in short -by specifying such constraint you indicate that only value types could be used as generic type parameter, for instance int, double, etc.

    In current implementation of Distinct and StructEqualityComparer this really does not make any difference, but idea of StructEqualityComparer is to compare structs, its name saying this, so all classes/method which are passing through own generic type parameters required for StructEqualityComparer obligated redefine the same generic type constraints as well. In your case Distinct() method passign its ownt T parameter so obligated to redefine all constraints.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from

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.