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

The Archive Base Latest Questions

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

This feels like a too easy question to be found with google, I think/hope

  • 0

This feels like a too easy question to be found with google, I think/hope I’ve got stuck in the details when trying to implement my own version of it. What I’m trying to do is to sort a list of MyClass objects depending on my Datatype object different search functions should be used.

I’ve had something like this in mind for the class Datatype:

class Datatype {
  public delegate int CMPFN(object x, object y);
  private CMPFN compareFunction;

  (...)

  private XsdDatatype((...), CMPFN compareFunction) {
      (...)
      this.compareFunction = compareFunction;
  }

  public CMPFN GetCompareFunction() {
    return this.compareFunction;
  }

  static private int SortStrings(object a, object b) {
      return ((MyClass)a).GetValue().CompareTo(((MyClass)b).GetValue());
  }
}

And later on I’m trying to sort a MyClass list something like this:

List<MyClass> elements = GetElements();
Datatype datatype = new Datatype((...), Datatype.SortStrings);
elements.Sort(datatype.GetCompareFunction()); // <-- Compile error!

I’m not overly excited about the cast in Datatype.SortStrings but it feels like this could work(?). The compiler however disagrees and gets me this error on the last line above and I’m a bit unsure exactly why CMPFN can’t be converted/casted(?) to IComparer.

Cannot convert type 'proj.Datatype.CMPFN' to 'System.Collections.Generic.IComparer<proj.MyClass>'
  • 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-15T17:30:19+00:00Added an answer on May 15, 2026 at 5:30 pm

    Delegates aren’t duck-typed like that. You can create an Comparison<MyClass> from a CMPFN but you can’t use a plain reference conversion – either implicit or explicit.

    Three options:

    • Create the comparer like this:

      elements.Sort(new Comparison<MyClass>(datatype.GetCompareFunction()));
      
    • Use a lambda expression to create a Comparison<T> and use that instead:

      elements.Sort((x, y) => datatype.GetCompareFunction()(x, y));
      
    • Write an implementation of IComparer<MyClass> which performs the comparison based on a CMPFN

    Note that the second approach will call GetCompareFunction once per comparison.

    A much better solution would be to get rid of CMPFN entirely – why not just use (or implement) IComparer<MyClass> to start with? Note that that would remove the casts as well. (If you’re happy using delegates instead of interfaces, you could express the comparison as a Comparison<MyClass> instead.)

    Note that as of .NET 4.5, you can use Comparer.Create to create a Comparer<T> from a Comparison<T> delegate.

    I’m not sure why your current API is in terms of object, but you should be aware that in C# 3 and earlier (or C# 4 targeting .NET 3.5 and earlier) you wouldn’t be able to convert an IComparer<object> into an IComparer<MyClass> (via a reference conversion, anyway). As of C# 4 you can, due to generic contravariance.

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

Sidebar

Related Questions

I feel like this is a stupid question because it seems like common sense
I feel like this is a dumb question and I'm missing something, but I
I feel like I should know this, but I haven't been able to figure
I'm sorry, but this is beginning to feel like kicking myself in the head.
I've seen a few examples of RSS Feeds in ASP.NET MVC, like this ,
This is a question I intend to answer myself, but please feel free to
Disclaimer: This is not actually a programming question, but I feel the audience on
First let me say that I really feel directionless on this question. I am
i think i sort of know the answer to this, but there are always
Having a heckuva time with this one, though I feel I'm missing something obvious.

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.