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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:01:25+00:00 2026-06-11T17:01:25+00:00

There is a set of classes that I do not own – I cannot

  • 0
  • There is a set of classes that I do not own – I cannot change them.
  • I’d like to add an identifying parameter to each, using an existing field in each class that has one.
  • So, I created a set of extension methods to fetch this field from each class, with a default for any class that does not have a specific implementation.

This works just fine when accessing the new extension method directly (the first three writes in the example below), but when the instances are first passed into a generic method, then the extension method selected is always the one for object (the second three writes).

Am I doing something wrong, or is this a limitation of the C# compiler?

public class Call { public string Number { get; set; } }
public class Message { public string Address { get; set; } }
public class Unknown { }

public static class Extensions
{
    public static string ID(this object item) { return "Unknown"; }
    public static string ID(this Call item) { return item.Number; }
    public static string ID(this Message item) { return item.Address; }
}

internal class Program
{
    private static void Main()
    {
        var call = new Call { Number = "555-1212" };
        var msg = new Message { Address = "you@email.com" };
        var other = new Unknown();

        // These work just as I would expect
        // - printing out Number, Address, or the default
        System.Console.WriteLine("Call = {0}", call.ID());
        System.Console.WriteLine("Message = {0}", msg.ID());
        System.Console.WriteLine("Unknown = {0}", other.ID());
        System.Console.WriteLine();

        // These all print out "Unknown"
        System.Console.WriteLine("Call = {0}", GetID(call));
        System.Console.WriteLine("Message = {0}", GetID(msg));
        System.Console.WriteLine("Unknown = {0}", GetID(other));
    }

    public static string GetID<T>(T item)
    {
        return item.ID();
    }
}
  • 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-06-11T17:01:27+00:00Added an answer on June 11, 2026 at 5:01 pm

    Overload resolution is performed at compile-time. The compiler knows nothing about T, so the only applicable overload is this one:

    public static string ID(this object item) { return "Unknown"; }
    

    If you want to effectively perform overload resolution at execution time, and if you’re using C# 4, you might want to consider using dynamic – which unfortunately doesn’t support extension methods directly:

    public static string GetID(dynamic item)
    {
        return Extensions.ID(item);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to make a list of classes behave like a set
Is there a set of command-line options that will convince gcc to produce a
Is there a set of things that every JavaScript programmer should know to be
Is there a set of functions that give you the file and/or the folder
Is there any super set or language similar to javascript that provides a more
I have some classes in C++ that I would like to expose to Lua.
I have a set of classes, each of which need to decide at some
I have a set of classes that manage db storage in a class hierarchy
I have a model with 15 properties and indexed=False set for each of them.
I have a collection of my own PointOfInterest classes (approx. 1500) each having their

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.