Guest User
nullable types example
By Guest User in .NET on Aug 01 2014
  • Raghav Mehra
    Aug, 2014 26

    Nullable types is essential as value type doesn't hold a null valueDeclaring Nullable Type1. Append a question mark, ?, to the type nameDateTime? startDate;2. Either you can assign a normal value or may assign a null value to it;strDate = null;ORstrDate = DateTime.Now;Working with Nullable Typesbool isNull = strDate == null;Console.WriteLine("isNull: " + isNull);The above example shows that you only need to use the equals operator to check for null. You could also make the equality check as part of an if statement, like this:int products;if (unitsInStock == null){products = 0;}else{products = (int)unitsInStock;}Note: Notice the cast operator in the else clause above. An explicit conversion is required when assigning from nullable to non-nullable types.Fortunately, there's a better way to perform the same task, using the coalesce operator, ??, shown below:int availableUnits = unitsInStock ?? 0;The coalesce operator works like this: if the first value (left hand side) is null, then C# evaluates the second expression (right hand side).

    • 0
  • Rama
    Aug, 2014 15

    Nullable types can store null values to the data Type. For example , int age; In a form if the user skips the Age to enter, then the value of age is stored as Zero In this case if we declare as int? age; instead of int age; then the value of age will be null and not zero, which is meaningful.

    • 0
  • Guest User
    Aug, 2014 1

    Int? someID = null; If(someID.HasVAlue) { }

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS