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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:20:09+00:00 2026-05-18T00:20:09+00:00

On a few separate occasions, I have tried to coax the declared type out

  • 0

On a few separate occasions, I have tried to coax the declared type out of a variable, relatively far from its declaration, only to find out that typeof(T) only works on type names.

I was wondering if there would be any breaking changes to allow typeof(variable) as well.

For example, with this code:

class Animal { /* ... */ }
class Goat : Animal { /* ... */ }

/* ... */

var g = new Goat();
Animal a = g;

Console.WriteLine(typeof(Goat));
Console.WriteLine(typeof(Animal));
Console.WriteLine(g.GetType());
Console.WriteLine(a.GetType());

You get something like:

Goat
Animal
Goat
Goat

Why is it not possible to do this:

Console.WriteLine(typeof(g));
Console.WriteLine(typeof(a));

Goat
Animal

I have given the spec a cursory glance, and can’t find any conflict. I think that it would clear up the question ‘Why this type?’ when using the typeof operator.

I know that the compiler is capable, here. An implementation using extension methods is actually trivial:

public static Type TypeOf<T>(this T variable)
{
    return typeof(T);
}

But that feels dirty, abusing the type-inference of the compiler.

  • 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-18T00:20:10+00:00Added an answer on May 18, 2026 at 12:20 am

    Suppose it’s allowed:

    class Animal { }
    
    string Animal;
    Type t = typeof(Animal); // uh-oh!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few separate applications which are all launched purely through a main
I have a few classes in the class library (separate assembly). I referenced it
I have a few pages made with jquery mobile. These pages are all separate
I have a few separate application projects (EARs) with multiple EJBs that I want
I have a series of input elements each with a few <div> tags separate.
I have a few QGraphicsScene subclasses CustomScene all deriving from a common interface that
We have a form which has a few separate submit buttons which do different
I have a few Resque jobs running, each started in a separate terminal window
We currently have a few .Net applications on different domains with separate membership on
I'm having a few problems breaking out an MsBuild package+deploy command into two separate

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.