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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:23:45+00:00 2026-05-11T15:23:45+00:00

I have run into a problem with generics and new members. I wrote a

  • 0

I have run into a problem with generics and new members. I wrote a generic class which operates on an object of type ObjectA. ObjectB derives from ObjectA and hides a few of ObjectA’s members. When I supply the type of ObjectB as the type parameter to the generic class, I would expect that when I call any of the members hidden by ObjectB, I would be calling ObjectB’s implementation. However, the CLR still calls the hidden members (ObjectA’s implementation). This seems illogical because I explicitly provided the type of ObjectB to the generic class. Is this a problem with generics themselves, or am I doing something wrong?

Edit: Unfortunately, I do not have access to ObjectA’s source code and the member I want to override is not virtual. If I had access to ObjectA’s source code, I would make the member virtual, but as I cannot do so, my only option for ‘overriding’ the member is through the ‘new’ keyword.

class GenericClass<T> where T : ObjectA   {       public void DoWork(T item)       {           // When type parameter 'T' is ObjectB, should get ObjectB's implementation           item.Invoke();       }   }    class ObjectA   {     public void Invoke()       {           // A's implementation...       }   }  class ObjectB : ObjectA   {     public new void Invoke()       {           // B's implementation...       }   }  static void Main()   {       GenericClass<ObjectB> genericClass = new GenericClass<ObjectB>();       ObjectB objectB = new ObjectB();       genericClass.DoWork(objectB);   } 

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T15:23:45+00:00Added an answer on May 11, 2026 at 3:23 pm

    No. The calls generated by the compiler are to the members it knows about at compile-time. That’s the members exposed by ObjectA.

    Any reason you’re not using normal inheritance, with virtual/overridden methods?

    Here’s another example of the same kind of thing, by the way – the overloaded == operator for strings isn’t used, even though T is string in the call to Foo:

    using System;  class Test {     static bool Foo<T>(T first, T second)         where T : class     {         return first == second;     }      static void Main()     {         string x = 'hello';         string y = new string(x.ToCharArray());          Console.WriteLine(Foo(x, y));     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to jQuery and have run into a problem. I have a
I have run into a problem with generics in Java and I can't find
I am just starting with Generics in C# but have run into a problem
Im building a small webshop and have run into a problem. The company which
I have run into a problem with Eclipse Indigo, attempting to start up my
I have run into a problem trying to modify a form I myself have
I have run into a problem w/ my model for databinding in WPF. I
I have run into a problem... I'm trying to use QTKit in an application
I have run into this problem a few times and I'm not happy with
I'm developing an app and have run into a problem with asynchronous calls... Here's

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.