Explain the '?' in: int? a = null;

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

    #1

    Explain the '?' in: int? a = null;

    I'm learning c# and see a code example where a value of null is assigned to
    a variable:

    int? a = null;

    I could not find this in the docs. can someone please explain exactly what
    the '?' is? and where-all it can be used?

    Thanks.


    --
    moondaddy@nospa m.nospam


  • José Joye

    #2
    Re: Explain the '?' in: int? a = null;

    You should search for "nullable types" in documentation.

    In short this allow value types to have null assigned to them. It is a
    shorthand for creating a variable of the generic System.Nullable <T>
    structure type.

    -José


    "moondaddy" <moondaddy@nosp am.nospam> wrote in message
    news:Ok$jWLcPGH A.1216@TK2MSFTN GP14.phx.gbl...[color=blue]
    > I'm learning c# and see a code example where a value of null is assigned[/color]
    to[color=blue]
    > a variable:
    >
    > int? a = null;
    >
    > I could not find this in the docs. can someone please explain exactly[/color]
    what[color=blue]
    > the '?' is? and where-all it can be used?
    >
    > Thanks.
    >
    >
    > --
    > moondaddy@nospa m.nospam
    >
    >[/color]


    Comment

    • Kevin Yu [MSFT]

      #3
      RE: Explain the '?' in: int? a = null;

      Hi moondaddy,

      The question mark means that variable is a nullable type. Nullable types
      represent value-type variables that can be assigned the value of null. You
      cannot create a nullable type based on a reference type. (Reference types
      already support the null value.)

      You can check the following link for more information:

      http://msdn2.microsoft.com/en-us/library/1t3y8s4s.aspx

      Kevin Yu
      =======
      "This posting is provided "AS IS" with no warranties, and confers no
      rights."

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Explain the '?' in: int? a = null;

        moondaddy wrote:[color=blue]
        > I'm learning c# and see a code example where a value of null is assigned to
        > a variable:
        >
        > int? a = null;
        >
        > I could not find this in the docs. can someone please explain exactly what
        > the '?' is? and where-all it can be used?[/color]

        It's a new feature in C# 2.0. See
        http://www.pobox.com/~skeet/csharp/c.../nullable.html for more
        information.

        Jon

        Comment

        • moondaddy

          #5
          Re: Explain the '?' in: int? a = null;

          Thanks to all that replied. This helps and the links were good too!

          --
          moondaddy@nospa m.nospam
          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:1141287167 .592468.87380@z 34g2000cwc.goog legroups.com...[color=blue]
          > moondaddy wrote:[color=green]
          >> I'm learning c# and see a code example where a value of null is assigned
          >> to
          >> a variable:
          >>
          >> int? a = null;
          >>
          >> I could not find this in the docs. can someone please explain exactly
          >> what
          >> the '?' is? and where-all it can be used?[/color]
          >
          > It's a new feature in C# 2.0. See
          > http://www.pobox.com/~skeet/csharp/c.../nullable.html for more
          > information.
          >
          > Jon
          >[/color]


          Comment

          Working...