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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:09:42+00:00 2026-05-22T21:09:42+00:00

Mark Michaelis wrote in his book ( C# 4.0 Essentials ): class EntityBase<T> where

  • 0

Mark Michaelis wrote in his book (C# 4.0 Essentials):

class EntityBase<T> where T : IComparable<T>
{
    public virtual void Method<T>(T t)
      where T : IComparable<T>
    {
        // ...
    }
}

class Entity<T> : EntityBase<T> where T : IComparable<T>
{
    public override void Method<T>(T t)
        // Error: Constraints may not be
        // repeated on overriding members
    where T : IComparable<T>
    {
        // ...
    }
}

However, overriding members need to
conform to the “interface” defined in
the base class method. Additional
constraints could break polymorphism,
so they are not allowed and the type
parameter constraints on the override
method are implied.

Could somebody kindly explain to me what it means to break polymorphism? And in this example how could polymorphism break?

  • 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-22T21:09:43+00:00Added an answer on May 22, 2026 at 9:09 pm

    His example is confusing in part because of the (incorrect, IMO) re-use of T for a generic method within a generic type. The two T are not the same! So; let’s work with a non-generic type:

    class EntityBase
    {
        public virtual void Method<T>(T t)
          where T : IComparable<T>
        {
            // ...
        }
    }
    
    class Entity : EntityBase
    {
        public override void Method<T>(T t)
            // Error: Constraints may not be
            // repeated on overriding members
        where T : IComparable<T>, ISomethingElse
        {
            // ...
        }
    }
    

    Here I added ISomethingElse – and clearly the 2nd method could try to use features of this second T – however, the caller might be:

    EntityBase foo = GetEntity(); // is actually an Entity (sub-type) instance
    foo.Method<SomeType>();
    ...
    EntityBase GetEntity() { return new Entity(); }
    

    the base implementation does not enforce ISomethingElse, so the compiler does not complain that it isn’t implemented. So what does the overridden method do? Hence it can’t exist.

    However! If you do this at the type level instead, it does work, as for the concrete object to exist we know the constraint was enforced:

    class EntityBase<T> where T : IComparable<T>
    {
        public virtual void Method(T t)
        {
            // ...
        }
    }
    
    class Entity<T> : EntityBase<T>
       where T : IComparable<T>, ISomethingElse
    {
        public override void Method(T t)
        {
            // ... can use ISomethingElse features
        }
    }
    

    And a brief reminder – if you have a generic type with <T>, don’t also use <T> in a generic method; something more specific like TValue etc…

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

Sidebar

Related Questions

I’ve been reading Mark Seemann’s book Dependency Injection in .NET and his blog post
If I don't mark a method as virtual , will it be available to
Supposing I mark a method @Transactional(rollbackFor = PersistenceException.class) in my Service layer and have
#pragma mark Internal API I've seen this in a book called xcode_quick_tour_iphoneOS. Does someone
I'd like to mark a method as deprecated, so the people using it can
I've just finished Mark Seemann's book Dependency Injection in .NET and I'm now trying
Is it possible to mark some members of an Entity as 'ignored' in an
I've tried to mark multiple locations on my mapview. There is, a class extends
I like using question mark at the end of method/function names in other languages.
I currently mark collections in entity beans as eager to avoid getting a lazy

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.