Welcome to Strong Coders Community Sign in | Join | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    Join StrongTypes - Free Generics Help By Email
    Join StrongTypes

    Community Credit Winner

    View my MVP profile

    The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

    Subscribe to Rolog via My AOL
    Subscribe to Rolog via Bloglines


    Subscribe to Rolog via Feedburner

    Furl Rolog




    Subscribe to Rolog via Newsburst


    Subscribe to Rolog via Pluck

    Add Rolog to Technorati Favorites




    Thycotic Secret Server

.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Nullable Types

One of the weirder features in C# 2.0 is nullable types. My thinking is as follows: Why complicate things with Nullable, or its shorter version of DataType? (i.e. int?), instead of allowing null values to be assigned to those data types that don't have any way of handling null? Maybe it was a matter of not breaking the framework or introducing changes gradually. Also, code such as that below will cause the compiler to error as nullable types and their non-nullable type equivelent are treated as 2 different types.

int ? m_Value1; // The shorter version of Nullable m_Value1
m_Value1 = int .Parse(Request.QueryString[ "SomeNumberParam" ]);

int m_Value2;

if (m_Value1 != null ) // Nullable types can be checked for null
{
   m_Value2 = m_Value1; // Won't compile
   m_Value2 = ( int )m_Value1; // Will compile
}

Hopefully, in the next version of C#, nullable types will be replaced with the ability for their non-nullable versions to handle null, like below:

int m_Value1, m_Value2;
m_Value1 = int .Parse(Request.QueryString[ "SomeNumberParam" ]);

if (m_Value1 != null )
{
   m_Value2 = m_Value1;
}

Do you dare ask what this non-sense looks like in code form? In case you are wondering, I'm using the non-nullable form of the word form. Sorry if I've confused you by including the ? after form in order to form a question. I now present you with non-sense:

using

System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[

Serializable , StructLayout ( LayoutKind .Sequential), TypeDependency ( "System.Collections.Generic.NullableComparer`1" ), TypeDependency ( "System.Collections.Generic.NullableEqualityComparer`1" )]
public struct Nullable where T : struct
{
   private bool hasValue;
   internal T value;

   public Nullable(T value)
   {
      this .value = value;
      this .hasValue = true ;
   }

   public bool HasValue
   {
      get
      {
         return this .hasValue;
      }
   }

   public T Value
   {
      get
      {
         if (! this .HasValue)
         {
             ThrowHelper .ThrowInvalidOperationException( ExceptionResource .InvalidOperation_NoValue);
         }
         return this .value;
      }
   }

   public T GetValueOrDefault()
   {
      return this .value;
   }

   public T GetValueOrDefault(T defaultValue)
   {
      if (! this .HasValue)
      {
         return defaultValue;
      }
      return this .value;
   }

   public override bool Equals( object other)
   {
      if (! this .HasValue)
      {
         return (other == null );
      }
      if (other == null )
      {
         return false ;
      }
      return this .value.Equals(other);
   }

   public override int GetHashCode()
   {
      if (! this .HasValue)
      {
         return 0;
      }
      return this .value.GetHashCode();
   }

   public override string ToString()
   {
      if (! this .HasValue)
      {
         return "" ;
      }
      return this .value.ToString();
   }

   public static explicit operator T( Nullable value)
   {
      return value.get_Value();
   }

   public static implicit operator Nullable (T value)
   {
      return new Nullable (value);
   }
}
   

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Sunday, January 01, 2006 7:47 PMby Ryan Olshan
Filed under: ,

Comments

XIII said:

Hi,

personally I like the new ?? operator in C# 2.0:

you can use it like this:

m_Value2 = m_Value1 ?? 0;

Which is the same like

if(m_Value1.HasValue)
m_Value2 = m_Value1.Value;
else
m_Value2 = 0;

In my current project we use nullable types a lot to have an easier mapping between nullable columns in SQL Server 2005.

Grz, Kris.
# January 2, 2006 12:20 AM

Ryan Olshan said:

Nice. I hadn't noticed the ?? operator yet. Cool shortcut. I still think an ideal goal by the next version of C# should be to allow structs that don't have the capability to handle nulls to have that functionality. But for now, I guess something is better than nothing. I can live with it.
# January 2, 2006 12:38 AM
New Comments to this post are disabled