more about nullable types where the Value property is used

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

    #1

    more about nullable types where the Value property is used

    Hello!

    What purpose has this Value propery on nullable types because it possible
    to display their value without using it.
    For example:

    int? intNullable1 = 3;
    int? intNullable2 = 5;
    Console.WriteLi ne(result); // This display 8
    Console.WriteLi ne(result.Value ); // This also display 8 the same

    So I can't see any point in using this Value property on nullable types or
    have I missed something here perhaps
    that makes the Value property useful to have?

    //Tony




  • =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

    #2
    Re: more about nullable types where the Value property is used

    Tony Johansson wrote:
    Hello!
    >
    What purpose has this Value propery on nullable types because it possible
    to display their value without using it.
    For example:
    >
    int? intNullable1 = 3;
    int? intNullable2 = 5;
    Console.WriteLi ne(result); // This display 8
    Console.WriteLi ne(result.Value ); // This also display 8 the same
    >
    So I can't see any point in using this Value property on nullable types or
    have I missed something here perhaps
    that makes the Value property useful to have?
    >
    //Tony
    >
    >
    >
    >
    try this:

    public void Test(Int32 value)
    {
    }

    Test(intNullabl e1);
    Test(intNullabl e1.Value);

    --
    Lasse Vågsæther Karlsen
    mailto:lasse@vk arlsen.no
    Blogger is a blog publishing tool from Google for easily sharing your thoughts with the world. Blogger makes it simple to post text, photos and video onto your personal or team blog.

    PGP KeyID: 0xBCDEA2E3

    Comment

    • Rene

      #3
      Re: more about nullable types where the Value property is used

      The code:

      Console.WriteLi ne(result);

      Is calling the "ToString() " method on the "result" variable. So its
      equivalent of you saying:

      Console.WriteLi ne(result.ToStr ing());

      Now, if you look at the internal implementation of the "ToString() " method
      on the "Nullable" variable type (using Reflector) is implemented the
      following way:

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

      So as you can see, you were calling the "Value" property, you just didn't
      know you were doing it.



      "Tony Johansson" <johansson.ande rsson@telia.com wrote in message
      news:glc5k.197$ U5.499@newsb.te lia.net...
      Hello!
      >
      What purpose has this Value propery on nullable types because it possible
      to display their value without using it.
      For example:
      >
      int? intNullable1 = 3;
      int? intNullable2 = 5;
      Console.WriteLi ne(result); // This display 8
      Console.WriteLi ne(result.Value ); // This also display 8 the same
      >
      So I can't see any point in using this Value property on nullable types or
      have I missed something here perhaps
      that makes the Value property useful to have?
      >
      //Tony
      >
      >
      >
      >

      Comment

      Working...