Tuesday 22 April 2014

Nullable Types in .Net C#

Leave a Comment
Since .NET version 2.0, the new feature introduce which is nullable types.  In the basic programming, how do you assign null to the promitive type such as int, long, and bool. Its is impossible.

The following code shows how to define an integer as nullable type and checks if the value of the integer is null or not.

Example 

/// <summary>
/// Nullable types
/// </summary>
public static void TestNullableTypes()
{
   // Syntax to define nullable types
   int? counter;
   counter = 100;

   if (counter != null)
   {

      // Assign null to an integer type
      counter = null;
      Console.WriteLine("Counter assigned null.");
   }
   else
   {

      Console.WriteLine("Counter cannot be assigned null.");
      Console.ReadLine();
   }
}



If you remove the "?" from int? counter, you will see a warning as following:



By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.