Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#/.NET: Is `typeof(variable)` a possible language feature?

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.

like image 718
John Gietzen Avatar asked Nov 08 '10 15:11

John Gietzen


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

I think the problem here is that .GetType() is older than typeof(). There used to be a day in C# where you needed to do

"0".GetType()

in order to get the String type (for example), until typeof() was born. I think if the concept had been part of the original language design, then indeed it might work as you describe. Due to typeof() being a late introduction to the language, then the designers had to make a choice: Obsolete/deprecate/remove .GetType() (and in the process make many, many uses of it obsolete), make typeof() overlap in functionality with GetType() (which is what you are asking), or make typeof()'s usage not overlap with GetType(). I think the C# people simply chose not to make the functionality overlap (to keep things simple and clear), and so typeof() was restricted in the way it is today.

like image 103
Gabriel Magana Avatar answered Oct 12 '22 23:10

Gabriel Magana