How can I convert decimal? to decimal

后端 未结 5 1020
-上瘾入骨i
-上瘾入骨i 2021-02-02 05:31

may be it is a simple question but I\'m try all of conversion method! and it still has error! would you help me?

decimal? (nullable decimal) to decimal

5条回答
  •  梦如初夏
    2021-02-02 06:08

    You don't need to convert a nullable type to obtain its value.

    You simply take advantage of the HasValue and Value properties exposed by Nullable.

    For example:

    Decimal? largeValue = 5830.25M;
    
    if (largeValue.HasValue)
    {
        Console.WriteLine("The value of largeNumber is {0:C}.", largeValue.Value);
    }
    else
    {
        Console.WriteLine("The value of largeNumber is not defined.");
    }
    

    Alternatively, you can use the null coalescing operator in C# 2.0 or later as a shortcut.

提交回复
热议问题