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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:52:15+00:00 2026-06-04T16:52:15+00:00

It looks like in the following case the polymorphism does not work properly I

  • 0

It looks like in the following case the polymorphism does not work properly
I have the following definitions:

interface BaseInterface{}
interface NewInterface:BaseInterface{}
class NewClass:NewInterface{}

class GenericClass<T> where T:BaseInterface
{
    public string WhoIAm(T anObject)
    {
        return TestPolymorphism.CheckInterface(anObject);
    }
}

class ImplementedClass:GenericClass<NewInterface>{}

class TestPolymorphism
{
    public static string CheckInterface(BaseInterface anInterface)
    {
        return "BaseInterface";
    }

    public static string CheckInterface(NewInterface anInterface)
    {
        return "NewInterface";
    }
}

Then when I call :

NewClass nc = new NewClass();
ImplementedClass impClass = new ImplementedClass();
Console.WriteLine("The result is " + impClass.WhoIAm(nc));

I have “The result is BaseInterface”

I was expecting to have “The result is NewInterface” as nc implement BaseClass and NewClass
What would be the best way to get “NewClass” as the result ?

Thanks

  • 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-04T16:52:16+00:00Added an answer on June 4, 2026 at 4:52 pm

    Keep in mind with generic methods, that non-virtual method calls are still resolved at compile time of the generic itself, not at the compile time of the realization of the generic.

    Thus this:

    class GenericClass<T> where T:BaseInterface
    {
        public string WhoIAm(T anObject)
        {
            return TestPolymorphism.CheckInterface(anObject);
        }
    }
    

    Will resolve to the overload that takes a BaseInterface because that’s what you’ve constrained it to regardless of the type T actually is.

    Generics in C# aren’t quite like templates in C++, in C# generics, all reference types share the same code for the generic, thus they all treat the generic type similarly at compile time. This means that any compile time overloads your generic invokes using the generic type placeholder can only go by any constraints you provide on the generic type itself, since generics are compiled before they are actually realized.

    That is, your GenericClass<T> is compiled before any usage of it is considered (which is very different from the way C++ does templates — both methods have their pros and cons). So, if you have an unconstrained generic (say, just T) then it is considered object for the purposes of overloading (roughly speaking), but if you have a constrained generic (say where T : BaseInterface) then it is considered BaseInterface for the purposes of overloading.

    You’d see something similar in this case:

    public static bool UberEquals<T>(T left, T right) where T : class
    {
        return left == right;
    }
    

    So you’d think if you called this by:

    var s1 = "ello";
    var s2 = "Hello";
    
    UberEquals<string>('H' + s1, s2);
    

    Since T is type string then it would call strings == overload, but it doesn’t, because you haven’t constrained T, thus at compile time it assumes a least common denominator of object and uses objects == instead.

    Another way to think of this:

    BaseInterface bi = new ImplementedClass();
    
    var x = TestPolymorphism.CheckInterface(bi);
    

    X will always say BaseInterface in the above, since overloads are resolved at compile time, not dynamically at run-time. Very similar to generics, just keep in mind that the generic is compiled before it is realized, so it can only go on whatever base class or interface you constrain it to for the purposes of overload resolution.

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

Sidebar

Related Questions

I have something that looks like the following: [CoolnessFactor] interface IThing {} class Tester
I have a class that looks like the following: public class MyClass { private
I have a template class which looks like the following: template <template <class TypeT>
I created a hierarchy of case objects in Scala that looks like the following:
I am writing unit for a class which looks like following using nunit and
I have XML that looks like the following: <?xml version=1.0 encoding=UTF-8 standalone=yes?> <ObjectList> <object
I have something that looks like the following: var someList = MockRepository.GenerateStub<IList<ISomething>>(); someList.Add(MockRepository.GenerateStub<ISomething>()); The
I have code that looks like the following: //unrelated code snipped resolver.reset(new tcp::resolver(iosvc)); tcp::resolver::query
I have code that looks like the following: <form id=MyForm name=MyForm method=post action=index.php> <input
I have a data set that looks like the following. EMPLID PHONE_TYPE PHONE ------

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.