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 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'm trying to build my first generic list and have run into some problems.
it seems I have run into a problem with Internet Explorer 7. I have
I'm using SWFUpload to handle file uploads and have run into a problem where
I've just started to use linq to sql and have run into a problem
I have run into a bit of a tricky problem in some C++ code,
I have run into a common, yet difficult problem. I do not use Photoshop
I often run into the problem that I have one stream full of data
Has anyone run into this problem... In my layout.phtml I have: <head> <?= $this->headTitle('Control
I have run in to a bit of a problem and I have done
I have run into an issue with WPF and Commands that are bound to

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.