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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:53:05+00:00 2026-05-16T19:53:05+00:00

I know this is probably a really simple question but I’m having a brain

  • 0

I know this is probably a really simple question but I’m having a brain fart at the moment. I am trying to create a method that can take one of 2 custom types. Basically the body of this method will be identical for both the types as they both have a Name property (I’m doing a comparison on the Name property to use in sorting). How should I do this?

My first thought was just to overload the method with the two types as arguments:

int Compare(Type1 first, Type1 second)
int Compare (Type2 first, Type2 second)

but the body of the methods ended up being identical thus it seems like a waste.

My next thought was to use generics but that doesn’t seem right because I’m not really making it generic as it can only be used with 2 specific types.

Clarification: The “custom” types are actually not my custom types. What I meant was taht they are not built-in types. I do not have control over what is in these types or the inheritence hierarchy. They just both happen to have the Name property.

  • 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-16T19:53:07+00:00Added an answer on May 16, 2026 at 7:53 pm

    Generic is still an option if the two types derive from the same base or use the same interface, and no other classes use the base and/or interface.

    int Compare<T>(T first, T second) where T : IHasName
    

    Short of that, having the two overloads is the way to go.

    Note: If you have .NET 4, you could make it a runtime thing and make the method dynamic.

    int Compare(dynamic first, dynamic second)
    

    In which case anything you throw at it will compile, but it will blow up at runtime if the Name property is not present.

    Personally, if I can’t modify the type to use a common interface, then I would go with the overloads and get compile-time safety.

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

Sidebar

Related Questions

This is probably a simple question, but I really don't know what I'm doing
I know this probably really simple but Im not sure what im doing wrong...
I know this is probably a really amateur question, but I can't figure this
I know this is probably a very simple question but how would I do
I know this probably has been asked before but I am having issues with
I know this is probably something simple but I can't seem to find anything
I know this is probably super easy, but I'm having a hard time wading
Dang-I know this is a subjective question so will probably get booted off/locked, but
This is probably really simple, and the answer is probably really obvious, but I'm
I know this isn't a straightforward question but I would really appreciate some feedback

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.