-3

What are the primitive typenames in C#? Yes, we have a similar answer, but that only mention types, not the typenames: https://stackoverflow.com/a/13530321/3691653

E.g. it mentions types such as System.Int32 and System.Single but not typenames int and float which also compiles for me. So what is the full list of typenames including its aliases?

In other words, I am interested about non-nullable typenames / typenames that require the Nullable<> keyword in order to be able to be set as null.

Community
  • 1
  • 1
Thomas
  • 816
  • 5
  • 16
  • First, that answer defines all the 'typenames' you may want, just read the third item in the list. Second, there is no such thing as 'typename' specifically distinct from type — e.g., `System.Int32` is a _.NET type_, `int` is a C# alias for that type. They are essentially the same thing. Third, why not trying search the [C# reference](https://msdn.microsoft.com/en-us/library/ya5y69ds.aspx) before asking? – Gasper Oct 16 '16 at 00:04

3 Answers3

1

Like these:

alias   type (in System namespace)

byte    Byte
sbyte   SByte
short   Int16
ushort  UInt16
int     Int32
uint    UInt32
long    Int64
ulong   UInt64
decimal Decimal
float   Single
double  Double

Read the rest at https://msdn.microsoft.com/en-us/library/ya5y69ds.aspx

John Alexiou
  • 28,472
  • 11
  • 77
  • 133
0

Primitive types and typenames that require the Nullable<> are two totally separate things.

Anything that is declared a struct instead of as a class is non nullable and requires Nullable<> to be wrapped around it to be set null.

A "Complete List" is impossible because anyone can write their own structs.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
0

If all you want to know is whether a given Type can be wrapped into a Nullable<> or not, then it's equivalent to asking whether it is a value type or not:

        Type someType = typeof(int); // then, try to change this to "typeof(string)"
        Type someTypeNullable = null;
        if (someType.IsValueType)
        {
            someTypeNullable = typeof(Nullable<>).MakeGenericType(someType);
        }
        if (someTypeNullable != null)
        {
            Console.WriteLine("nullable version: " + someType + "?");
        }
        else
        {
            Console.WriteLine(someType + " is a reference type");
        }
YSharp
  • 1,066
  • 9
  • 8