How can I find out what data type some variable is holding? (e.g. int, string, char, etc.)
I have something like this now:
private static void Main()
{
var someone = new Person();
Console.WriteLine(someone.Name.typeOf());
}
public class Person
{
public int Name { get; set; }
}
Other answers offer good help with this question, but there is an important and subtle issue that none of them addresses directly. There are two ways of considering type in C#: static type and run-time type.
Static type is the type of a variable in your source code. It is therefore a compile-time concept. This is the type that you see in a tooltip when you hover over a variable or property in your development environment.
Run-time type is the type of an object in memory. It is therefore a run-time concept. This is the type returned by the
GetType()method.An object’s run-time type is frequently different from the static type of the variable, property, or method that holds or returns it. For example, you can have code like this:
The static type of the variable is
object, but at run time, the type of the variable’s referent isstring. Therefore, the next line will print "System.String" to the console:But, if you hover over the variable
oin your development environment, you’ll see the typeSystem.Object(or the equivalentobjectkeyword).For value-type variables, such as
int,double,System.Guid, you know that the run-time type will always be the same as the static type, because value types cannot serve as the base class for another type; the value type is guaranteed to be the most-derived type in its inheritance chain. This is also true for sealed reference types: if the static type is a sealed reference type, the run-time value must either be an instance of that type ornull.Conversely, if the static type of the variable is an abstract type, then it is guaranteed that the static type and the runtime type will be different.
To illustrate that in code:
Another user edited this answer to incorporate a function that appears below in the comments, a generic helper method to use type inference to get a reference to a variable’s static type at run time, thanks to
typeof:You can use this function in the example above:
But this function is of limited utility unless you want to protect yourself against refactoring. When you are writing the call to
GetStaticType, you already know that o’s static type is object. You might as well writeThis reminds me of some code I encountered when I started my current job, something like
instead of