简体   繁体   中英

Differentiation between typeof(int) and typeof(Int32) in C# possible?

got a simple question, and I think I know the answer but I wan't to hear it from some other people.

Let's say I'll create a generic function:

public string GetTypeName<T>()
{
}

It should return the name of the type T, which is easy:

return typeof(T).Name

At this point we can ignore, that this does not work for generic types, that's not the subject of the question.

My problem is: If someone calls the function like that:

GetTypeName<int>()

It should return "int".

If someone calls the function like that:

GetTypeName<Int32>()

It should return "Int32".

But I think thats not possible huh?

Thanks for answering!

The distinction is not possible, because the distinction does not exist.

int is a keyword which the compiler interprets to mean the global::System.Int32 type as a shortcut or syntactic sugar . In other words, at runtime and within the .NET framework itself, there is no int type, only Int32 .

The same applies to all the types which have keywords - see MSDN for the complete list and their equivalents

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM