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

  • Anthony Cumia

    Anthony Cumi

    5 EYLÜL 2006
  • Eric Anthony

    Eric Anthony

    13 AĞUSTOS 2011
  • SellerDp

    SellerDp

    27 EKİM 2009