.NET Interview Questions – What are the nullable variable?

What problem resolved?

As everyone knows the value type (structs) not allow null as a value, this quire that if we declare a variable of type int for example you will never be able a comparison for null because this variable does not support null as a value. A possible elegant solution to this problem would define our own Integer structure to support null values as I did in the example below.

public struct NullInteger
{
  private int _value;
  private bool _hasValue;

  public NullInteger ( int value)
  {
     _value = value;
     _hasValue = true ;
  }

  public int Value
  {
     get
     {
         if (!_hasValue )
             throw new InvalidOperationException ( "not defined a value for this variable" );
         return _value;
     }
     set
     {
         _value = value;
         _hasValue = true ;
     }
  }

  public bool HasValue
  {
      get { return _hasValue; }
  }

  public static implicit operator NullInteger ( int value)
  {
      return new NullInteger (value);
  }

  public static NullInteger Null
  {
      get { return new NullInteger (); }
  }
 }

As we can see basically add two properties to the structure, one flag to validate if there is or not value and the other value in itself. Thus the consultation of this structure would be something like this.

static void Main ( string [] args)
{
NullInteger variable = 3;
NullInteger variable2 = Entero.Null;

Console.WriteLine (variable.HasValue ? variable.Value.ToString (): "null" );
Console.WriteLine (variable2.HasValue ? variable2.Value.ToString (): "null" );
Console.ReadKey ();
}

Now this workaround works fine but it has a high cost for us. For this reason the .net framework defined a new nullable data type which basically accepts null values.

How to define the types nullables?

Well basically  the way to define a nullable data is adding the operator ? the data type that we are specifying as I did in the following example.

static void Main ( string [] args)
{
int ? vari = 3;
int ? vari2 = null ;

Console.WriteLine (vari.HasValue ? vari.Value.ToString (): "null" );
Console.WriteLine (vari2.HasValue ? vari2.Value.ToString (): "null" );
Console.ReadKey ();
}

As you can see the use is very similar to what we had previously defined so basically we don’t need to create our own data type to support null values. On the other hand when using Nullables value we can use of the operator ?? because Nullable <T> offers several methods to make use of this as we can see below.

static void Main ( string [] args)
{
int ? vari = 3;

value = vari.HasValue? vari.Value: 7;
value = vari.GetValueOrDefault (7);
value = vari ?? 7;
Console.WriteLine (vari.HasValue ? vari.Value.ToString (): &quot;null&quot; );
Console.ReadKey ();
}

With any of the three options previously showed we can see that depending on the value of the variable we will assign the value that has or failing another value (or default).
As you can see this data type gives us the flexibility to work with properties that might have null values.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s