nullable types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    nullable types

    Hello!

    These two declarations(1 and 2) are the same I assume.
    1. System.Nullable <intnullable;
    2. System.Nullable <intnullable = new System.Nullable <int();

    So because these 1 and 2 are the same is no point to use the longer
    declaration as 2 it good enough
    to use decaration 1.

    Do you agree with me?
    The declaration of nullable above a value type or a reference type?

    //Tony


  • =?Utf-8?B?S0g=?=

    #2
    RE: nullable types

    The first one can't be used until it is assigned a value, so

    Nullable<intnul lable;
    Console.WriteLi ne( nullable.HasVal ue ); // Compiler error here

    Nullable<intnul lable = new Nullable<int>() ;
    Console.WriteLi ne( nullable.HasVal ue ); // OK

    You can also use the ? shorthand in C# for nullable types:

    int? nullable = null; // same as second example above

    HTH


    "Tony Johansson" wrote:
    Hello!
    >
    These two declarations(1 and 2) are the same I assume.
    1. System.Nullable <intnullable;
    2. System.Nullable <intnullable = new System.Nullable <int();
    >
    So because these 1 and 2 are the same is no point to use the longer
    declaration as 2 it good enough
    to use decaration 1.
    >
    Do you agree with me?
    The declaration of nullable above a value type or a reference type?
    >
    //Tony
    >
    >
    >

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: nullable types

      Tony Johansson <johansson.ande rsson@telia.com wrote:
      These two declarations(1 and 2) are the same I assume.
      1. System.Nullable <intnullable;
      2. System.Nullable <intnullable = new System.Nullable <int();
      Not for local variables. A local variable declared as per 1 has no
      definitely assigned value. In other words, this won't compile:

      void Invalid()
      {
      System.Nullable <intnullable;
      if (nullable == null)
      {
      Console.WriteLi ne("Null");
      }
      }

      whereas this will:

      void Valid()
      {
      System.Nullable <intnullable = new System.Nullable <int>;
      if (nullable == null)
      {
      Console.WriteLi ne("Null");
      }
      }
      So because these 1 and 2 are the same is no point to use the longer
      declaration as 2 it good enough
      to use decaration 1.
      Well, I'd generally use:

      int? nullable;
      or
      int? nullable = null;

      The latter is potentially slightly clearer to readers.
      Do you agree with me?
      The declaration of nullable above a value type or a reference type?
      A nullable value type is still a value type. When the null literal is
      used (as above) with a nullable value type, it represents the "null
      value" of the type, which is the value where HasValue returns false.

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      Working...