By 22/07/2015 12 Comments

C# Nullable Types

  • In C# we cannot initialize any value data type with an undefined or null value such as int seatNumber = null, because in C# these value types are non-nullable by default.
  • We can assign null values by marks these types as nullable types in C# by using nullable type which is denoted  by a (?)  Question mark.
  • Assigning null values to Nullable data types are valid and acceptable after making these types as nullable types.
  • Nullable types are very useful when we work with databases.
  • Nullable data types has these 2 read only properties 1 – HasValue and 2 – Value, where HasValue is a bool property which returns true or false, if nullable type has a value that property returns true otherwise false. At the other hand is a Value property which contains the nullable type’s value.

Example:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // Initialize a nullable int data type zipCode with null value.
            int? zipCode = null;

            // Re-initialize nullable type zipCode with new value 5566.
            zipCode = 5566;

            // Print HasValue nullable type property.
            Console.WriteLine("Has nullable type value?: {0}", zipCode.HasValue);
            
            // Print nullable value through its Value property.
            Console.WriteLine("That value is: {0}", zipCode.Value);

            Console.ReadKey();

        } // End of main method definition. 
    } // End of class. 
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .