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

The Archive Base Latest Questions

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

Confusing question, I know. Given the following: class Test { public static void GenericFunc<T>(T

  • 0

Confusing question, I know. Given the following:

    class Test
    {
        public static void GenericFunc<T>(T SomeType)
        {
            System.Console.WriteLine("typeof(T): " + typeof(T).Name);
            System.Console.WriteLine("SomeType.GetType(): " + SomeType.GetType().Name);
        }
    }

    public class BaseType
    {
        public void RunTest() { Test.GenericFunc(this); }
    }

    public class DerivedType : BaseType { }

The following code produces interesting output:

    DerivedType Derived = new DerivedType();
    Derived.RunTest();

    // output:
    // typeof(T): BaseType
    // SomeType.GetType(): DerivedType

However, this behaves as I would expect:

    Test.GenericFunc(new Derived());

    // output:
    // typeof(T): DerivedType
    // SomeType.GetType(): DerivedType

Can anyone help me understand the mechanism here that’s causing T to be evaluated as BaseType in the first case?

Thanks in advance!

  • 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-23T18:12:46+00:00Added an answer on May 23, 2026 at 6:12 pm

    Because this:

    public class BaseType
    {
        public void RunTest() { Test.GenericFunc(this); }
    }
    

    Is basically equivalent to:

    public class BaseType
    {
        public void RunTest() { Test.GenericFunc<BaseType>(this); }
    }
    

    Therefore the GenericFunc gets instatiated at compile time with T = BaseType. However the this object you are passing in at runtime is the derived type which you get by SomeType.GetType().

    In the second case the compiler infers the type as DerivedType directly from the usage and therefore GenericFunc gets instatiated with T = DerivedType.

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

Sidebar

Related Questions

I am a beginner of python and have a question, very confusing for me.
Excuse my title if it's confusing, but I don't know how else to describe
I find System.Monitor very confusing, although I understand threading, locks, deadlocks, race conditions, dining
Sorry but this is really confusing me and I know the answer is staring
Possible Duplicate: Cross-browser CSS Ok, so the question might be somehow a little confusing.
This is a little confusing to explain, so bear with me here... I want
I've seen a confusing behavior regarding the MSI files generated by a VDPROJ file.
This has been confusing me for some time. With the advent of UTF-8 as
I'm getting some confusing results around myCustom.ashx handler. If i visit the handler via
Title might be a bit confusing, so let me explain. I have a website

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.