Why typeof(string).FullName
is giving System.String
and not string
? The same is with all other "simple" types like int
, float
, double
, ...
I understand that typeof
is returning the System.Type
object for the given type, but why is string
not also be an System.Type
object ?
Is it because string
is part of the c# language, and System.Type
is part of the system libraries?
Because string
is an alias for System.String
. Your string
C# code is converted on compile time to System.String
. This is the same for other aliases.
In C#, string
is just an alias for System.String
, so both are the same and typeof
returns the same type object.
The same goes for all other primitive types. For example, int
is just an alias for System.Int32
.
If you need to get the shorter C# alias name of a type, you can use CSharpCodeProvider.GetTypeOutput()
instead of FullName
:
using Microsoft.CSharp;
[...]
var compiler = new CSharpCodeProvider();
var type = new CodeTypeReference(typeof(Int32));
Console.WriteLine(compiler.GetTypeOutput(type)); // Prints int
(code snippet taken from this question)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With