C#中值类型如何设置为null

datetime是值类型,是不允许设为null的。微软为了解决这个问题,为值类型引入了可空类型。
如:
int型对应的是Nullable<int> 简写是:int?
DateTime对应的是Nullable<DateTime>,简写是DateTime?

你这个问题其实可以将time声明成可空类型,如果文本框有值则time = Convert.ToDateTime(txt_registertime.Text);否则time = null。
在其他获取time值的地方可以先判断它是不是有值:
if( time.HasValue ){}

posted @ 2017-08-01 13:17  刘家小生  阅读(2617)  评论(0编辑  收藏  举报