SORU
7 Temmuz 2009, Salı


Nasıl DateTime dönüştürmek için? DateTime

Null bir DateTime (DateTime?) ama bir hata alıyorum DateTime, bir dönüştürmek istiyorum

"Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)"

Aşağıdaki çalıştılar:

DateTime UpdatedTime = (DateTime)_objHotelPackageOrder.UpdatedDate == null 
    ? DateTime.Now : _objHotelPackageOrder.UpdatedDate;

CEVAP
7 Temmuz 2009, Salı


Kullanmak istediğiniz tam olarak bu amaç için tasarlanmıştır null-coalescing operator,.

Bunu kullanarak bu kod ile sonuna kadar.

DateTime UpdatedTime = _objHotelPackageOrder.UpdatedDate ?? DateTime.Now;

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • dhcrr's channel

    dhcrr's chan

    2 Ocak 2007
  • Flash CS6 Video Tutorials for Beginners (Actionscript 3 Gaming)

    Flash CS6 Vi

    14 EYLÜL 2012
  • guau . .

    guau . .

    25 Ocak 2008