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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:03:37+00:00 2026-05-26T16:03:37+00:00

I understand how to use the IComparer interface with helper classes which provide custom

  • 0

I understand how to use the IComparer interface with helper classes which provide custom ways to sort. For example, here is a typical example which is very much like all the examples I have seen on the web including Microsoft’s online help page:

// This helper class is used to sort an array of people by name, 
// where 'Person' is a class type.
public class PeopleNameComparer : IComparer
{
  // Test the name of each object.
  int IComparer.Compare(object o1, object o2)
  {
     Person p1 = o1 as Person;
     Person p2 = o2 as Person;
     if (p1 != null && p2 != null)
        return string.Compare(p1.Name, p2.Name);
     else
        throw new ArgumentException("Parameter is not a Person!");
  }
}

I also understand that if we have an array of type Person (myPeople), we can then sort this array with:

Array.Sort(myPeople, new PeopleNameComparer());

In this case we are creating a new PeopleNameComparer object which is of type IComparer and passing it into the Array.Sort() method as a second parameter.

Now to make things neater, we can implement a property to provide the object user with a more friendly way to invoke the custom sort:

public static IComparer SortByName
{ get { return (IComparer)new PeopleNameComparer(); } }

What I don’t understand with this kind of Property is why do all the examples use the (IComparer) cast to cast the newly created helper class(PeopleNameComparer in this example) into an IComparer object when this object is already of type IComparer? I have tried without the cast and the code appears to work fine:

// This property seems to work fine without the cast?
public static IComparer SortByName
{ get { return new PeopleNameComparer(); } }

I could understand it if the ‘new’ keyword returned a plain vanilla System.Object type which would then have to be casted to the appropriate IComparer, but just cannot see the need for the cast here.
But I followed Microsoft’s example, and my example is similar to an example in my Pro C# book.

Is there any reason why a cast would be necessary here?

  • 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-26T16:03:37+00:00Added an answer on May 26, 2026 at 4:03 pm

    Using an explicit cast is more explicit. Pardon the truism.. but it’s just that. It helps make the code more readable.

    In some cases explicit casts can help the runtime disambiguate a cast if there are multiple possible options but that doesn’t seem to occur with return type. Only in expressions. Following is a common example where you would need an explicit cast in an expression:

    public class StringEnumerable : IEnumerable, IEnumerable<String>
    {
        IEnumerator<String> IEnumerable<String>.GetEnumerator()
        {
            yield return "TEST";
        }
    
        public IEnumerator GetEnumerator()
        {
            // without the explicit cast of `this` to the generic interface the 
            // method would call itself infinitely until a StackOverflowException occurs
            return ((IEnumerable<String>)this).GetEnumerator();
        }
    }
    

    If you remove the explicit cast from non-generic interface implementation it will cause an infinite loop.

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

Sidebar

Related Questions

So I understand the use of the ActionLink HTML helper, as well as most
I'm trying to understand the use cases for which implementing oAuth as a service
Example: $this->getResponse() ->appendBody('Hello' . $name) In the previous example, I understand the use of
I can understand the use for one level of namespaces. But 3 levels of
I want to understand the use of xargs man in Rampion's code : screen
I don't really understand the use and concept of an Intent. I DO understand
I am learning Django and I am trying to understand the use of models.py
I am a competent C++ developer. I understand and use polymorphism, templates, the STL,
I understand how to use ClickOnce and deploy an application to a web site
I understand how to use REST for doing general entity interactions - using urls

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.