Blog ( Of Ryan Olshan)

Recent Posts

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

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

Receive Email Updates


.NET Websites

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

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • 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
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

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

Sunday, January 01, 2006 - Posts

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: Jan 01 2006, 07:47 PM by Ryan Olshan | with 2 comment(s)
Filed under: ,
More Posts