Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What is this line?

What is this line?

Scheduled Pinned Locked Moved C#
question
10 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    helo guys... I am frequently wathcing the following line in tutorials online. What does this line mean? thnx

    public int? Year;

    M V R 3 Replies Last reply
    0
    • A AmbiguousName

      helo guys... I am frequently wathcing the following line in tutorials online. What does this line mean? thnx

      public int? Year;

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      int is non nullable int? is nullable

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • A AmbiguousName

        helo guys... I am frequently wathcing the following line in tutorials online. What does this line mean? thnx

        public int? Year;

        V Offline
        V Offline
        V 0
        wrote on last edited by
        #3

        to elaborate on Mycroft's reply. This is often used when talking to the database. Suppose you have a column that contains a number, but the column can be null. In that case you would need to check if the value was DBNUll.Value and if so convert your variable (that maps to that column) to -1 or 0 or something. When using the ? construct you don't need to convert it and can directly map the variable to the database column. Some goes if you want to write a value to that column. The ? is an 'override' so you can put null values in non-nullable variables (int, double, DateTime, ... but not strings, they can be null) Hope this helps. [EDIT]PS: don't be afraid to use it, but don't use it if you don't have to.[\EDIT]

        V.

        1 Reply Last reply
        0
        • A AmbiguousName

          helo guys... I am frequently wathcing the following line in tutorials online. What does this line mean? thnx

          public int? Year;

          R Offline
          R Offline
          Roger Wright
          wrote on last edited by
          #4

          The ? after the variable type renders a variable that is nullable. What value that may have I haven't discovered, as I like my variables to have real values before I use them, but apparently someone has decided that a null integer is a good thing, and thoughtfully provided a way to declare such a beast. The ? operator can be used with any type, for what it's worth.

          Will Rogers never met me.

          L R B 3 Replies Last reply
          0
          • R Roger Wright

            The ? after the variable type renders a variable that is nullable. What value that may have I haven't discovered, as I like my variables to have real values before I use them, but apparently someone has decided that a null integer is a good thing, and thoughtfully provided a way to declare such a beast. The ? operator can be used with any type, for what it's worth.

            Will Rogers never met me.

            L Offline
            L Offline
            Lukasz Nowakowski
            wrote on last edited by
            #5

            Roger Wright wrote:

            The ? operator can be used with any type, for what it's worth.

            No. It can only be used with value types (int, double, decimal, DateTime). It cannot be used with reference types. And it cannot be used with strings.

            Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

            P R 2 Replies Last reply
            0
            • L Lukasz Nowakowski

              Roger Wright wrote:

              The ? operator can be used with any type, for what it's worth.

              No. It can only be used with value types (int, double, decimal, DateTime). It cannot be used with reference types. And it cannot be used with strings.

              Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Part of the reason it can't be used with strings is that strings are nullable by default. They don't need special treatment.

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              L 1 Reply Last reply
              0
              • P Pete OHanlon

                Part of the reason it can't be used with strings is that strings are nullable by default. They don't need special treatment.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                L Offline
                L Offline
                Lukasz Nowakowski
                wrote on last edited by
                #7

                I just checked, cause I wasn't sure... string is just a reference type, that acts like value type. So it can point to a null reference of course. My mistake ;-)

                Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

                1 Reply Last reply
                0
                • L Lukasz Nowakowski

                  Roger Wright wrote:

                  The ? operator can be used with any type, for what it's worth.

                  No. It can only be used with value types (int, double, decimal, DateTime). It cannot be used with reference types. And it cannot be used with strings.

                  Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

                  R Offline
                  R Offline
                  Roger Wright
                  wrote on last edited by
                  #8

                  I didn't know about the reference type limitation, but then, I can't think of any value for the concept anyway and have never needed it. Strings are nullable by nature, so they don't count. Thanks for the info! :-D

                  Will Rogers never met me.

                  1 Reply Last reply
                  0
                  • R Roger Wright

                    The ? after the variable type renders a variable that is nullable. What value that may have I haven't discovered, as I like my variables to have real values before I use them, but apparently someone has decided that a null integer is a good thing, and thoughtfully provided a way to declare such a beast. The ? operator can be used with any type, for what it's worth.

                    Will Rogers never met me.

                    R Offline
                    R Offline
                    RobCroll
                    wrote on last edited by
                    #9

                    ? can be useful to determine if a choice has been made. To give an example, a person has 0 children because 1. they truely don't have any children or because 2. The value was never set? Null is a way of explicitly determining if the person has no children. The old way was to set the default to -1. I think null is a better way of expressing that the value was not set.

                    "You get that on the big jobs."

                    1 Reply Last reply
                    0
                    • R Roger Wright

                      The ? after the variable type renders a variable that is nullable. What value that may have I haven't discovered, as I like my variables to have real values before I use them, but apparently someone has decided that a null integer is a good thing, and thoughtfully provided a way to declare such a beast. The ? operator can be used with any type, for what it's worth.

                      Will Rogers never met me.

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #10

                      To expand on the last reply, it is useful when a variable can validly take the whole range of the type in question, and can also have a 'not set' value. Typically we have either used an invalid value (e.g. testing getchar() for -1 in old school C; -1 is a common 'not set' value in ints still) or created another boolean variable to define whether or not we set a value:

                      bool customerLikedProduct;
                      bool customerResponded;

                      People have also solved that one with a three way enum ...

                      enum NullableBool { True, False, Maybe }

                      ... but when you do that you lose the ability to use boolean logical operators, compatibility with external data sources or applications, etc. In these cases, you would like to be able to represent the 'not set' value within one variable. That's what nullable types give you:

                      bool? customerLikedProduct;

                      As already mentioned, it's particularly useful for working with databases where null values are a common and standard thing. For me, using -1 or double.NaN is fine, as long as that can never be a valid value for the variable in question. But there are times when the whole valid range for the natural data type is valid and you should use nullable types.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups