What is equivalent of 'null'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • COHENMARVIN@lycos.com

    What is equivalent of 'null'

    I'm a VB programmer learning C#. One thing I don't know is how to
    test whether an object is 'null' or not. There is no 'null' keyword
    in C#.
    Why is that?
    CM
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: What is equivalent of 'null'

    CM,

    I think you have it the other way around. There most definitely is a
    null keyword in C#, but not one in VB. In VB, the equivalent is 'Nothing'.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    <COHENMARVIN@ly cos.comwrote in message
    news:a2d8ed69-92a7-425d-9113-bc32bc441059@28 g2000hsw.google groups.com...
    I'm a VB programmer learning C#. One thing I don't know is how to
    test whether an object is 'null' or not. There is no 'null' keyword
    in C#.
    Why is that?
    CM

    Comment

    • Cowboy \(Gregory A. Beamer\)

      #3
      Re: What is equivalent of 'null'

      There is a null:

      if(var == null)
      //Do something

      If you are talking nullable types, you have to test against the type, not
      against null, as nullable types are a bit of a kludge. There are also
      certain types that cannot be null, unless made with a nullable type.

      //default = null, but not the keyword
      int? nullableInt;

      //default = 0
      int nonNullableint;

      --
      Gregory A. Beamer
      MVP, MCP: +I, SE, SD, DBA

      *************** *************** *************** ****
      | Think outside the box!
      |
      *************** *************** *************** ****
      <COHENMARVIN@ly cos.comwrote in message
      news:a2d8ed69-92a7-425d-9113-bc32bc441059@28 g2000hsw.google groups.com...
      I'm a VB programmer learning C#. One thing I don't know is how to
      test whether an object is 'null' or not. There is no 'null' keyword
      in C#.
      Why is that?
      CM

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: What is equivalent of 'null'

        On Feb 28, 3:21 pm, "Cowboy \(Gregory A. Beamer\)"
        <NoSpamMgbwo... @comcast.netNoS pamMwrote:
        Yep. I thought about that later and realized I was being overstressed, and,
        therefore, stupid.
        >
        if(!nullableInt .HasValue)
        {
        >
        }
        >
        is the correct value for nullable types.
        It certainly gives the right result. I think I'd use

        if (nullableInt==n ull)

        for the sake of idiom, but each to their own :)
        The rest of the answer was, in
        fact, correct, as many types have no concept of null. An int is the perfect
        example here.
        Sure - but for every non-nullable type there's a nullable equivalent.

        Jon

        Comment

        • Cowboy \(Gregory A. Beamer\)

          #5
          Re: What is equivalent of 'null'

          I agree completely on the "for every non-nullable", but it is a bit of a
          kludge in many ways. :-)

          Thanks for watchdogging. I am using the newsgroups as a stress release and
          sometimes the stress is way too high that my brain does not work. Between
          the job and Miranda's cancer, it has been a rough few months. Sincerely,
          thanks for keeping me honest on this one.

          --
          Gregory A. Beamer
          MVP, MCP: +I, SE, SD, DBA

          *************** *************** *************** ****
          | Think outside the box!
          |
          *************** *************** *************** ****
          "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
          news:015180c1-c33d-43d9-bae1-29babe856cb2@2g 2000hsn.googleg roups.com...
          On Feb 28, 3:21 pm, "Cowboy \(Gregory A. Beamer\)"
          <NoSpamMgbwo... @comcast.netNoS pamMwrote:
          >Yep. I thought about that later and realized I was being overstressed,
          >and,
          >therefore, stupid.
          >>
          >if(!nullableIn t.HasValue)
          >{
          >>
          >}
          >>
          >is the correct value for nullable types.
          >
          It certainly gives the right result. I think I'd use
          >
          if (nullableInt==n ull)
          >
          for the sake of idiom, but each to their own :)
          >
          >The rest of the answer was, in
          >fact, correct, as many types have no concept of null. An int is the
          >perfect
          >example here.
          >
          Sure - but for every non-nullable type there's a nullable equivalent.
          >
          Jon

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: What is equivalent of 'null'

            On Feb 28, 3:37 pm, "Cowboy \(Gregory A. Beamer\)"
            <NoSpamMgbwo... @comcast.netNoS pamMwrote:
            I agree completely on the "for every non-nullable", but it is a bit of a
            kludge in many ways. :-)
            Sort of. I can't think of a more elegant solution though. Admittedly
            it's slightly worrying that different languages have different
            operator behaviour - equality of nullable types in VB is different to
            that in C#, for example.

            What I'd like to see is non-nullable reference types. That could be
            useful in a number of ways - although given the large body of code
            already out there, it's probably too late.
            Thanks for watchdogging. I am using the newsgroups as a stress release and
            sometimes the stress is way too high that my brain does not work. Between
            the job and Miranda's cancer, it has been a rough few months. Sincerely,
            thanks for keeping me honest on this one.
            So long as you'll do me the favour in return :)

            (I worry sometimes that people don't want to correct MVPs. Everyone is
            highly fallible, IME.)

            Jon

            Comment

            • Cowboy \(Gregory A. Beamer\)

              #7
              Re: What is equivalent of 'null'


              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:f32d58d4-cd0a-4efb-9d33-74351db7e4be@i1 2g2000prf.googl egroups.com...
              On Feb 28, 3:37 pm, "Cowboy \(Gregory A. Beamer\)"
              <NoSpamMgbwo... @comcast.netNoS pamMwrote:
              >I agree completely on the "for every non-nullable", but it is a bit of a
              >kludge in many ways. :-)
              >
              Sort of. I can't think of a more elegant solution though. Admittedly
              it's slightly worrying that different languages have different
              operator behaviour - equality of nullable types in VB is different to
              that in C#, for example.
              This is a bigger issue than many think, and not just on nullable types. When
              you bounce from language to language, you often find that the different way
              features are handled causes you to get into situations where you end up
              answering a question incorrectly for one answer that would be correct in
              another.

              I spend so little time in VB any more, but there are occasions where someone
              has me examine some VB code and I have to.
              What I'd like to see is non-nullable reference types. That could be
              useful in a number of ways - although given the large body of code
              already out there, it's probably too late.
              I am not sure it would do much for me, but perhaps I will get more time to
              ponder it later. I have, however, set up reference types that had default
              values, so perhaps that would be an reason for a non-nullable reference
              types.

              NOTE: I am not fond of hard coded initialization of values, but it was a
              better option than the client had done intially, which was chain behavior to
              a constructor.
              >Thanks for watchdogging. I am using the newsgroups as a stress release
              >and
              >sometimes the stress is way too high that my brain does not work. Between
              >the job and Miranda's cancer, it has been a rough few months. Sincerely,
              >thanks for keeping me honest on this one.
              >
              So long as you'll do me the favour in return :)
              >
              (I worry sometimes that people don't want to correct MVPs. Everyone is
              highly fallible, IME.)

              When I look back a year ago at some code, I think "man I sucked then". The
              truth is, I will probably say the same in another six months to a year. And,
              this is a good thing, as it means I am growing. The day I look at old code
              and say "man, that is the best I have ever done" is the day I should go out
              to pasture. :-)

              --
              Gregory A. Beamer
              MVP, MCP: +I, SE, SD, DBA

              *************** *************** *************** ****
              | Think outside the box!
              |
              *************** *************** *************** ****


              Comment

              • RobinS

                #8
                Re: What is equivalent of 'null'


                "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                news:f32d58d4-cd0a-4efb-9d33-74351db7e4be@i1 2g2000prf.googl egroups.com...
                On Feb 28, 3:37 pm, "Cowboy \(Gregory A. Beamer\)"
                <NoSpamMgbwo... @comcast.netNoS pamMwrote:
                >I agree completely on the "for every non-nullable", but it is a bit of a
                >kludge in many ways. :-)
                >
                Sort of. I can't think of a more elegant solution though. Admittedly
                it's slightly worrying that different languages have different
                operator behaviour - equality of nullable types in VB is different to
                that in C#, for example.
                >
                What I'd like to see is non-nullable reference types. That could be
                useful in a number of ways - although given the large body of code
                already out there, it's probably too late.
                >
                >Thanks for watchdogging. I am using the newsgroups as a stress release
                >and
                >sometimes the stress is way too high that my brain does not work. Between
                >the job and Miranda's cancer, it has been a rough few months. Sincerely,
                >thanks for keeping me honest on this one.
                >
                So long as you'll do me the favour in return :)
                >
                (I worry sometimes that people don't want to correct MVPs. Everyone is
                highly fallible, IME.)
                >
                Jon
                Oh, don't worry, Jon. We're all just sitting here waiting with bated breath
                for you to make a mistake so we can correct you. <g>

                Still waiting...


                Still waiting... Sigh.

                RobinS.
                GoldMail, Inc.

                Comment

                • KWienhold

                  #9
                  Re: What is equivalent of 'null'

                  On 28 Feb., 16:57, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
                  What I'd like to see is non-nullable reference types. That could be
                  useful in a number of ways - although given the large body of code
                  already out there, it's probably too late.
                  I think that would be a really useful feature, in addition to
                  parameters and return values, it would also be nice to be able to
                  specify it for properties.
                  Maybe you should add this to your C# 4 wishlist, who knows, they might
                  listen ;)

                  Kevin Wienhold

                  Comment

                  Working...