C#
  Home arrow C# arrow Page 2 - More About Generics
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

More About Generics
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-12-18

    Table of Contents:
  • More About Generics
  • 4.6 Creating a Value Type That Can Be Initialized to Null
  • 4.7 Reversing the Contents of a Sorted List
  • 4.8 Making Read-Only Collections the Generic Way

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    More About Generics - 4.6 Creating a Value Type That Can Be Initialized to Null


    (Page 2 of 4 )

     

    Problem

    You have a variable that is a numeric type, which will hold a numeric value obtained from a database. The database may return this value as a null. You need a simple, clean way to store this numeric value, even if it is returned as a null.

    Solution

    Use a nullable value type. There are two ways of creating a nullable value type. The first way is to use the ? type modifier:

      int? myDBInt = null;

    The second way is to use theNullable<T>generic type:

      Nullable<int> myDBInt = new Nullable<int>();

    Discussion

    Both of the following statements are equivalent:

      int? myDBInt = null;
      Nullable<int> myDBInt = new Nullable<int>();

    In both cases,myDBIntis a nullable type and is initialized tonull.

    A nullable type implements theINullableValueinterface, which has two read-only property members,HasValueandValue. TheHasValueproperty returnsfalseif the nullable value is set tonull; otherwise, it returnstrue. IfHasValuereturnstrue, you can access theValue property, which contains the currently stored value. IfHasValue returnsfalseand you attempt to read theValueproperty, you will get anInvalidOperationExceptionthrown. This is because theValueproperty is undefined at this point. Below is an example of a test of nullable value using theHasValue property value:

      if (myDBInt.HasValue)
          Console.WriteLine("Has a value: " + myDBInt.Value);
      else
          Console.WriteLine("Does not have a value (NULL)");

    In addition, one can simply compare the value tonull, as shown below:

      if (myDBInt != null)
          Console.WriteLine("Has a value: " + myDBInt.Value);
      else
          Console.WriteLine("Does not have a value (NULL)");

    Either method is acceptable.

    When casting a nullable value to a non-nullable value, the cast operates as it would normally, except when the nullable type is set tonull. In this case, anInvalidOperationExceptionis thrown. When casting a non-nullable value to a nullable value, the cast operates as it would normally. NoInvalidOperationExceptionwill be thrown, as the non-nullable value can never benull.

    The tricky thing to watch out for with nullable types is when comparisons are performed. For example, if the following code is executed:

      if (myTempDBInt < 100)
          Console.WriteLine("myTempDBInt < 100");
      else
          Console.WriteLine("myTempDBInt >= 100");

    The text “myTempDBInt >= 100” is displayed, which is obviously incorrect if the value ofmyTempDBInt isnull. To fix this code, you have to check ifmyTempDBIntisnull. If it is not, you can execute theifstatement in the previous code block:

      if (myTempDBInt != null)
      {
         if (myTempDBInt < 100)
             Console.WriteLine("myTempDBInt < 100");
         else
            
    Console.WriteLine("myTempDBInt >= 100");
      }
      else
      {
        
    // Handle the null here.
      }

    Another interesting thing about nullable types is that you can use them in expressions similar to normal numeric types, for example:

      int? DBInt = 10;
      int Value = 2;
      int? Result = DBInt + Value; // Result == 12

    The result of using a nullable value in most operators is anull if any nullable value isnull.

    Neither the comparison operators nor the null coalescing operator lift to nullable.

    However, if none of the nullable values isnull, the operation is evaluated as it normally would be. IfDBInt, for example, were set tonull, the value placed inResult would also benull.

    See Also

    The “Nullable<T>Generic Class” and “Using Nullable Types” topics in the MSDN documentation.

    More C# Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "C# 3.0 Cookbook, Third Edition," published...
     

    Buy this book now. This article is excerpted from chapter four of the C# 3.0 Cookbook, Third Edition, written by Jay Hilyard and Stephen Teilhet (O'Reilly, 2008; ISBN: 059651610X). Check it out today at your favorite bookstore. Buy this book now.

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek