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. Small confusion in Nullable Type

Small confusion in Nullable Type

Scheduled Pinned Locked Moved C#
question
7 Posts 6 Posters 1 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.
  • R Offline
    R Offline
    Ravi Sant
    wrote on last edited by
    #1

    Declaring bool? b = true; Then, b.GetType(); should return Nullable or Null? Why does it has type bool? I am not clear on this. Your know' will be helpful. :)

    L T H L 4 Replies Last reply
    0
    • R Ravi Sant

      Declaring bool? b = true; Then, b.GetType(); should return Nullable or Null? Why does it has type bool? I am not clear on this. Your know' will be helpful. :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      From http://msdn.microsoft.com/en-us/library/ms366789%28VS.80%29.aspx[^]: However, if you attempt to obtain type information from Nullable variables at runtime using the GetType method or the is operator, the result is a Type object that represents the underlying type, not the Nullable type itself. Calling GetType on a Nullable type causes a boxing operation to be performed when the type is implicitly converted to Object. Therefore GetType always returns a Type object that represents the underlying type, not the Nullable type.

      P R 2 Replies Last reply
      0
      • R Ravi Sant

        Declaring bool? b = true; Then, b.GetType(); should return Nullable or Null? Why does it has type bool? I am not clear on this. Your know' will be helpful. :)

        T Offline
        T Offline
        TweakBird
        wrote on last edited by
        #3

        RaviSant wrote:

        should return Nullable or Null?

        return Null See these links will help you to get idea. Nullable Types[^] Using Nullable Types[^]

        1 Reply Last reply
        0
        • R Ravi Sant

          Declaring bool? b = true; Then, b.GetType(); should return Nullable or Null? Why does it has type bool? I am not clear on this. Your know' will be helpful. :)

          H Offline
          H Offline
          Hiren solanki
          wrote on last edited by
          #4

          RaviSant wrote:

          b.GetType();

          It will return underlying datatype only.

          RaviSant wrote:

          should return Nullable or Null?

          No. It's just prone to accept null values that's it.

          Regards, Hiren.

          My Recent Article: - Way to know which control have raised a postback
          My Recent Tip/Trick: - Remove HTML Tag, get plain Text

          1 Reply Last reply
          0
          • L Lost User

            From http://msdn.microsoft.com/en-us/library/ms366789%28VS.80%29.aspx[^]: However, if you attempt to obtain type information from Nullable variables at runtime using the GetType method or the is operator, the result is a Type object that represents the underlying type, not the Nullable type itself. Calling GetType on a Nullable type causes a boxing operation to be performed when the type is implicitly converted to Object. Therefore GetType always returns a Type object that represents the underlying type, not the Nullable type.

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

            Excellent answer. Clear, concise and accurate - just the kind I like.:thumbsup:

            I'm not a stalker, I just know things. Oh by the way, you're out of milk.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            • L Lost User

              From http://msdn.microsoft.com/en-us/library/ms366789%28VS.80%29.aspx[^]: However, if you attempt to obtain type information from Nullable variables at runtime using the GetType method or the is operator, the result is a Type object that represents the underlying type, not the Nullable type itself. Calling GetType on a Nullable type causes a boxing operation to be performed when the type is implicitly converted to Object. Therefore GetType always returns a Type object that represents the underlying type, not the Nullable type.

              R Offline
              R Offline
              Ravi Sant
              wrote on last edited by
              #6

              Good Reply. Thank You :)

              1 Reply Last reply
              0
              • R Ravi Sant

                Declaring bool? b = true; Then, b.GetType(); should return Nullable or Null? Why does it has type bool? I am not clear on this. Your know' will be helpful. :)

                L Offline
                L Offline
                Laxman Auti
                wrote on last edited by
                #7

                RaviSant wrote:

                Declaring bool? b = true;

                The intention of nullable types is to allow/accept null value. So when we declare "bool?" then, it is of type "Boolean" and can accept null value. You can check if the variable has the value by using "HasValue" property of variable. if it is true then get the value of variable using "Value" property.

                Knock out 't' from can't, you can if you think you can. :cool:

                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