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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:57:02+00:00 2026-05-15T01:57:02+00:00

I noticed in C#, unlike C++, you can combine virtual and generic methods. For

  • 0

I noticed in C#, unlike C++, you can combine virtual and generic methods. For example:

using System.Diagnostics;

class Base {
    public virtual void Concrete() {Debug.WriteLine("base concrete");}
    public virtual void Generic<T>() {Debug.WriteLine("base generic");}
}

class Derived : Base {
    public override void Concrete() {Debug.WriteLine("derived concrete");}
    public override void Generic<T>() {Debug.WriteLine("derived generic");}
}

class App {
    static void Main() {
        Base x = new Derived();
        x.Concrete();
        x.Generic<PerformanceCounter>();
    }
}

Given that any number of versions of Generic<T> could be instantiated, it doesn’t look like the standard vtbl approach could be used to resolve method calls, and in fact it’s not. Here’s the generated code:

        x.Concrete();
mov         ecx,dword ptr [ebp-8] 
mov         eax,dword ptr [ecx] 
call        dword ptr [eax+38h] 
        x.Generic<PerformanceCounter>();
push        989A38h 
mov         ecx,dword ptr [ebp-8] 
mov         edx,989914h 
call        76A874F1 
mov         dword ptr [ebp-4],eax 
mov         ecx,dword ptr [ebp-8] 
call        dword ptr [ebp-4] 

The extra code appears to be looking up a dynamic vtbl according to the generic parameters, and then calling into it. Has anyone written about the specifics of this implementation? How well does it perform compared to the non-generic case?

  • 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-15T01:57:03+00:00Added an answer on May 15, 2026 at 1:57 am

    The .NET generics implementation can handle such a scenario easily and with very good performance. I have written a blog post about it a while ago.

    One of the best resources for finding information about the how the CLR implements generics is this paper by Micosoft Research.

    You got the thing about the vtable right. How the CLR creates executable code for a generic type when the JIT compiler stumbles upon one, depends on the generic type parameter. The handling is different for value types and reference types.

    While the exectuable code (instantiation, the native image) is shared among instantiations for all generic type parameters that are reference types, the vtable associated with an instance (object) of the instantiation is unique to the concrete parameter type.

    Here’s the relevant quote from the above mentioned paper:

    4.2 Object representation Objects in the CLR’s garbage-collected heap are
    represented by a vtable pointer
    followed by the object’s contents
    (e.g. fields or array elements). The
    vtable’s main role is virtual method
    dispatch: it contains a code pointer
    for each method that is defined or
    inherited by the object’s class. But
    for simple class types, at least,
    where there is a one-to-one
    correspondence between vtables and
    classes, it can also be used to
    represent the object’s type. When the
    vtable is used in this way we call it
    the type’s type handle. In an
    implementation of polymorphism based
    on full specialization, the notion
    of exact run-time type comes for free
    as different instantiations of the
    same parameterized type have different
    vtables. But now suppose that code is
    shared between different in-
    stantiations such as List<string>
    and List<object>. The vtables for the
    two instantiations will be identical,
    so we need some way of representing
    the instantiation at run-time.

    …

    [For each instantiation, we ]Replace the vtable pointer by a pointer to a combined vtable-
    and-instantiation structure and duplicate it [the structure] per instantiation.

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

Sidebar

Related Questions

I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
I noticed some code of a colleague today that initialized class variables in the
I noticed that in Windows 7, unlike XP, if a program crashes that I
Given a class hierarchy where the base class defines a recursive self-type: abstract class
I was reading the C Standard the other day, and noticed that unlike signed
I noticed that Google maps is providing directions in my local language (hungarian) when
I noticed that my Designer.vb file of one of my forms has a lot
I noticed in another post, someone had done something like: double d = 3.1415;
I noticed a new web service today called a Dead man's switch , which
We noticed that lots of bugs in our software developed in C# (or Java)

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.