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

  • Joshua Benedict

    Joshua Bened

    26 EKİM 2013
  • Marina and The Diamonds

    Marina and T

    8 Temmuz 2008
  • wolfys you tube

    wolfys you t

    22 Kasım 2006