Whats the use of Nullable.GetUnderlyingType, if typeof(int?) is an Int32?

前端 未结 5 2099
萌比男神i
萌比男神i 2021-02-05 16:07

why is typeof int? an Int32

int? x = 1;
Console.WriteLine(x.GetType().Name);

If it is okay then what\'s the use of

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 16:33

    Its for when you don't know its Int32.

    Example:

        public Type GetNullableUnderlyingType(Nullable obj) 
            where T : struct
        {
            return Nullable.GetUnderlyingType(typeof(Nullable));
        }
    

    Here, you can pass any Nullable object and get it to return it's underlying type.

提交回复
热议问题