C#
  Home arrow C# arrow Page 2 - Iterators and Nullable Types
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#

Iterators and Nullable Types
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-10-23

    Table of Contents:
  • Iterators and Nullable Types
  • Nullable Types
  • Lifted Operators
  • Mixing nullable and nonnullable operators

  • 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


    Iterators and Nullable Types - Nullable Types


    (Page 2 of 4 )

    Null Basics

    Reference types can represent a nonexistent value with a null reference. Value types, however, cannot ordinarily represent null values. For example:

      string s = null;     // OK, Reference Type
      int i = null;        // Compile Error, Value Type cannot be null

    To represent null in a value type, you must use a special construct called a nullable type. A nullable type is denoted with a value type followed by the? symbol:

      int? i = null;                   // OK, Nullable Type
      Console.WriteLine (i == null);   // True

    Nullable<T> struct

    T? translates into System.Nullable<T>. Nullable<T> is a lightweight immutable structure, having only two fields, to represent Value and HasValue. The essence ofSystem.Nullable<T>is very simple:

      public struct Nullable<T> where T : struct
      {
        public T Value {get;}
        public bool HasValue {get;}
        public T GetValueOrDefault();
        public T GetValueOrDefault(T defaultValue);
        ...
     
    }

    The code:

      int? i = null;
      Console.WriteLine (i == null);          // true

    translates to:

      Nullable<int>i = new Nullable<int>();
      Console.WriteLine (! i.HasValue);       // true

    Attempting to retrieveValuewhenHasValueis false throws anInvalidOperationException.GetValueOrDefault()returnsValue ifHasValueis true; otherwise, it returnsnewT()or a specified custom default value.

    The default value ofT?isnull.

    Implicit and explicit nullable conversions

    The conversion from T to T? is implicit, and from T? to T is explicit. For example:

      int? x = 5;       // implicit
      int y = (int)x;   // explicit

    The explicit cast is directly equivalent to calling the nullable object’sValueproperty. Hence, anInvalidOperationExceptionis thrown ifHasValueis false.

    Boxing and unboxing nullable values

    When T? is boxed, the boxed value on the heap contains T, not T?. This optimization is possible because a boxed value is a reference type that can already express null.

    More C# Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "C# 3.0 in a Nutshell, Third Edition, A...
     

    Buy this book now. This article is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek