How can I get the primitive name of a type in C#?

EDIT: I was half wrong in the answer below.

Have a look at CSharpCodeProvider.GetTypeOutput. Sample code:

using Microsoft.CSharp;
using System;
using System.CodeDom;

class Test
{
    static void Main()
    {
        var compiler = new CSharpCodeProvider();
        // Just to prove a point...
        var type = new CodeTypeReference(typeof(Int32));
        Console.WriteLine(compiler.GetTypeOutput(type)); // Prints int
    }
}

However, this doesn’t translate Nullable<T> into T? – and I can’t find any options which would make it do so, although that doesn’t mean such an option doesn’t exist 🙂


There’s nothing in the framework to support this – after all, they’re C#-specific names.

(Note that string isn’t a primitive type, by the way.)

You’ll have to do it by spotting Nullable`1 yourself (Nullable.GetUnderlyingType may be used for this, for example), and have a map from the full framework name to each alias.

Leave a Comment