using System;
namespace somens
{
class GettingTypeName
{
static void MethodUsingVar()
{
var someInt = 0;
Console.WriteLine("someInt is a : {0}", someInt.GetType().FullName);
Console.WriteLine("someInt is a : {0}", someInt.GetType().Name); //Why .Name?
}
static void Main(string[] args)
{
MethodUsingVar();
Console.ReadLine();
}
}
}
//Output
//someInt is a : System.Int32;
//someInt is a : Int32;
I understand the usage of someInt.GetType().FullName
, since it "Gets the fully qualified name of the type,"(quote from Microsoft).
But I don't know why someInt.GetType().Name
would give Int32.
The Name
property of the returned Type
class "gets the name of the current member." (Another quote from the same Microsoft page https://msdn.microsoft.com/en-us/library/system.type(v=vs.110).aspx)
Since it is used for members' names of a particular type, how does the code output the correct type name (without namespace) instead? It just doesn't make sense.
Thanks
The "member" here isn't your variable - it's a member of the namespace (or assembly, or module if you want). (It's unfortunate that the documentation is inherited from MemberInfo
- if it said "the name of the type" it would be clearer.)
You're dealing with a Type
- you'll get the same Type
object whether you call GetType()
or use typeof(int)
. The type is the Int32
type in the System
namespace, so its full name is System.Int32
and its name is Int32
. The Type
object has no knowledge of your variable.
See more on this question at Stackoverflow