How can I convert decimal? to decimal

后端 未结 5 1017
-上瘾入骨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:07

    There's plenty of options...

    decimal? x = ...
    
    decimal a = (decimal)x; // works; throws if x was null
    decimal b = x ?? 123M; // works; defaults to 123M if x was null
    decimal c = x.Value; // works; throws if x was null
    decimal d = x.GetValueOrDefault(); // works; defaults to 0M if x was null
    decimal e = x.GetValueOrDefault(123M); // works; defaults to 123M if x was null
    object o = x; // this is not the ideal usage!
    decimal f = (decimal)o; // works; throws if x was null; boxes otherwise
    

提交回复
热议问题