null == int testing

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

    null == int testing

    In VB you can test if a value type has the "default" value by doing the
    following:

    dim i as integer
    if (Nothing = i) then
    '* i is really 0
    end if


    Now, when I try to do this in C# of course it doesn't work because you can't
    do:

    int i;
    if ( null == i) {
    // i is 0
    }

    of course because you can't compare null == i.

    Now, in this case I know, and you know, that an int defaults to 0, but there
    are lots of types that default to some value that I don't know, e.g.
    System.Drawing. Color.

    Can anyone tell me how I can check if a value type is equal to its default
    value?
  • jeremiah johnson

    #2
    Re: null == int testing

    You know you have an int, why don't you just set it to 0 when you create it?

    int i = 0;

    That way you know exactly what you're working with.

    jeremiah();

    cmay wrote:[color=blue]
    > In VB you can test if a value type has the "default" value by doing the
    > following:
    >
    > dim i as integer
    > if (Nothing = i) then
    > '* i is really 0
    > end if
    >
    >
    > Now, when I try to do this in C# of course it doesn't work because you can't
    > do:
    >
    > int i;
    > if ( null == i) {
    > // i is 0
    > }
    >
    > of course because you can't compare null == i.
    >
    > Now, in this case I know, and you know, that an int defaults to 0, but there
    > are lots of types that default to some value that I don't know, e.g.
    > System.Drawing. Color.
    >
    > Can anyone tell me how I can check if a value type is equal to its default
    > value?[/color]

    Comment

    • jeremiah johnson

      #3
      Re: null == int testing

      to re-answer your question, since i misread it last time, i'll say that
      the C# compiler, in every circumstance I have ever come across, will
      always assign a sensible default value when you declare an integer.

      if you have i declared at one point, and you mistakenly redeclare it,
      you'll get a warning from the compiler.

      just set all of your variables to some value that makes sense on creation.

      also in C# i believe that you must do

      if (i == null) {}

      rather than

      if (null == i) {}

      as you have shown, but i'm not sure.

      cmay wrote:[color=blue]
      > In VB you can test if a value type has the "default" value by doing the
      > following:
      >
      > dim i as integer
      > if (Nothing = i) then
      > '* i is really 0
      > end if
      >
      >
      > Now, when I try to do this in C# of course it doesn't work because you can't
      > do:
      >
      > int i;
      > if ( null == i) {
      > // i is 0
      > }
      >
      > of course because you can't compare null == i.
      >
      > Now, in this case I know, and you know, that an int defaults to 0, but there
      > are lots of types that default to some value that I don't know, e.g.
      > System.Drawing. Color.
      >
      > Can anyone tell me how I can check if a value type is equal to its default
      > value?[/color]

      Comment

      • Ted Miller

        #4
        Re: null == int testing

        This may or may not help you now, but in C# 2.0 you can do

        if (i == default(int)) ...

        Also, your canned example -- even if altered to compare against 0, such as
        "if (i == 0)" -- wouldn't even compile, because C# does not allow use of
        uninitialized variables.

        PS the idiom of putting the constant on the left-hand side of a comparison
        is not needed in C#, because there is no implicit conversion to bool from
        other types like there is in C/C++.


        "cmay" <cmay@discussio ns.microsoft.co m> wrote in message
        news:8E7FD3BD-7C17-4167-8FE0-F76A3E748AC7@mi crosoft.com...[color=blue]
        > In VB you can test if a value type has the "default" value by doing the
        > following:
        >
        > dim i as integer
        > if (Nothing = i) then
        > '* i is really 0
        > end if
        >
        >
        > Now, when I try to do this in C# of course it doesn't work because you
        > can't
        > do:
        >
        > int i;
        > if ( null == i) {
        > // i is 0
        > }
        >
        > of course because you can't compare null == i.
        >
        > Now, in this case I know, and you know, that an int defaults to 0, but
        > there
        > are lots of types that default to some value that I don't know, e.g.
        > System.Drawing. Color.
        >
        > Can anyone tell me how I can check if a value type is equal to its default
        > value?[/color]


        Comment

        • cody

          #5
          Re: null == int testing

          You can test if (i==new int()) or if (i==new MyStruct())

          The default constructor of a value type will always return a
          null-initialized struct.

          "cmay" <cmay@discussio ns.microsoft.co m> schrieb im Newsbeitrag
          news:8E7FD3BD-7C17-4167-8FE0-F76A3E748AC7@mi crosoft.com...[color=blue]
          > In VB you can test if a value type has the "default" value by doing the
          > following:
          >
          > dim i as integer
          > if (Nothing = i) then
          > '* i is really 0
          > end if
          >
          >
          > Now, when I try to do this in C# of course it doesn't work because you
          > can't
          > do:
          >
          > int i;
          > if ( null == i) {
          > // i is 0
          > }
          >
          > of course because you can't compare null == i.
          >
          > Now, in this case I know, and you know, that an int defaults to 0, but
          > there
          > are lots of types that default to some value that I don't know, e.g.
          > System.Drawing. Color.
          >
          > Can anyone tell me how I can check if a value type is equal to its default
          > value?[/color]


          Comment

          • Ted Miller

            #6
            Re: null == int testing

            This only works for value types that implement operator ==. It does not
            compile in the general case.

            "cody" <deutronium@gmx .de> wrote in message
            news:ucY9w%23ub FHA.3280@TK2MSF TNGP09.phx.gbl. ..[color=blue]
            > You can test if (i==new int()) or if (i==new MyStruct())
            >
            > The default constructor of a value type will always return a
            > null-initialized struct.
            >
            > "cmay" <cmay@discussio ns.microsoft.co m> schrieb im Newsbeitrag
            > news:8E7FD3BD-7C17-4167-8FE0-F76A3E748AC7@mi crosoft.com...[color=green]
            >> In VB you can test if a value type has the "default" value by doing the
            >> following:
            >>
            >> dim i as integer
            >> if (Nothing = i) then
            >> '* i is really 0
            >> end if
            >>
            >>
            >> Now, when I try to do this in C# of course it doesn't work because you
            >> can't
            >> do:
            >>
            >> int i;
            >> if ( null == i) {
            >> // i is 0
            >> }
            >>
            >> of course because you can't compare null == i.
            >>
            >> Now, in this case I know, and you know, that an int defaults to 0, but
            >> there
            >> are lots of types that default to some value that I don't know, e.g.
            >> System.Drawing. Color.
            >>
            >> Can anyone tell me how I can check if a value type is equal to its
            >> default
            >> value?[/color]
            >
            >[/color]


            Comment

            • Daniel O'Connell [C# MVP]

              #7
              Re: null == int testing

              > also in C# i believe that you must do[color=blue]
              >
              > if (i == null) {}
              >
              > rather than
              >
              > if (null == i) {}
              >
              > as you have shown, but i'm not sure.
              >[/color]


              Either is perfectly acceptable, the reasoning for null == i is just no
              longer there.


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: null == int testing

                Ted Miller <ted@millerzone .net> wrote:[color=blue]
                > This only works for value types that implement operator ==. It does not
                > compile in the general case.[/color]

                True. Change it to:

                if (i.Equals(new MyStruct())) and it works fine though.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Sherif El-Metainy

                  #9
                  Re: null == int testing

                  Then we can use Object.Equals, which will suffer performance penalties
                  becaue of boxing

                  Best regards,
                  Sherif
                  "Ted Miller" <ted@millerzone .net> wrote in message
                  news:11an7vh2h3 42df4@corp.supe rnews.com...[color=blue]
                  > This only works for value types that implement operator ==. It does not
                  > compile in the general case.
                  >
                  > "cody" <deutronium@gmx .de> wrote in message
                  > news:ucY9w%23ub FHA.3280@TK2MSF TNGP09.phx.gbl. ..[color=green]
                  >> You can test if (i==new int()) or if (i==new MyStruct())
                  >>
                  >> The default constructor of a value type will always return a
                  >> null-initialized struct.
                  >>
                  >> "cmay" <cmay@discussio ns.microsoft.co m> schrieb im Newsbeitrag
                  >> news:8E7FD3BD-7C17-4167-8FE0-F76A3E748AC7@mi crosoft.com...[color=darkred]
                  >>> In VB you can test if a value type has the "default" value by doing the
                  >>> following:
                  >>>
                  >>> dim i as integer
                  >>> if (Nothing = i) then
                  >>> '* i is really 0
                  >>> end if
                  >>>
                  >>>
                  >>> Now, when I try to do this in C# of course it doesn't work because you
                  >>> can't
                  >>> do:
                  >>>
                  >>> int i;
                  >>> if ( null == i) {
                  >>> // i is 0
                  >>> }
                  >>>
                  >>> of course because you can't compare null == i.
                  >>>
                  >>> Now, in this case I know, and you know, that an int defaults to 0, but
                  >>> there
                  >>> are lots of types that default to some value that I don't know, e.g.
                  >>> System.Drawing. Color.
                  >>>
                  >>> Can anyone tell me how I can check if a value type is equal to its
                  >>> default
                  >>> value?[/color]
                  >>
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: null == int testing

                    <"Sherif El-Metainy" <elmeteny REMOVETHIS at thewayout NOSPAM dot
                    net>> wrote:[color=blue]
                    > Then we can use Object.Equals, which will suffer performance penalties
                    > becaue of boxing[/color]

                    Using Object.Equals doesn't so much suffer performance penalties
                    because of boxing, but because the implementation of ValueType.Equal s
                    uses reflection to compare the value of each field in turn.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    • Bob Grommes

                      #11
                      Re: null == int testing

                      I would have to check to be sure, but I don't believe that "if Nothing = i"
                      works in VB.NET either unless you have Option Strict Off. Which you never
                      should.

                      An int is a value type and as such can never equal null (or Nothing in VB
                      parlance).

                      In any case if it equals zero, then it does not and can not equal something
                      else, such as null.

                      This is the sort of muddled thinking that turns me off to VB. A default
                      value is not an unknown value and the two should never be confused.

                      --Bob

                      "cmay" <cmay@discussio ns.microsoft.co m> wrote in message
                      news:8E7FD3BD-7C17-4167-8FE0-F76A3E748AC7@mi crosoft.com...[color=blue]
                      > In VB you can test if a value type has the "default" value by doing the
                      > following:
                      >
                      > dim i as integer
                      > if (Nothing = i) then
                      > '* i is really 0
                      > end if
                      >
                      >
                      > Now, when I try to do this in C# of course it doesn't work because you
                      > can't
                      > do:
                      >
                      > int i;
                      > if ( null == i) {
                      > // i is 0
                      > }
                      >
                      > of course because you can't compare null == i.
                      >
                      > Now, in this case I know, and you know, that an int defaults to 0, but
                      > there
                      > are lots of types that default to some value that I don't know, e.g.
                      > System.Drawing. Color.
                      >
                      > Can anyone tell me how I can check if a value type is equal to its default
                      > value?[/color]


                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: null == int testing

                        Bob Grommes <bob@bobgrommes .com> wrote:[color=blue]
                        > I would have to check to be sure, but I don't believe that "if Nothing = i"
                        > works in VB.NET either unless you have Option Strict Off. Which you never
                        > should.[/color]

                        Yes it does. Try this:

                        Option Strict On
                        Imports System

                        Public Class Test

                        Shared Sub Main()
                        Dim x as Integer = Nothing
                        Console.WriteLi ne(x)
                        End Sub
                        End Class

                        [color=blue]
                        > An int is a value type and as such can never equal null (or Nothing in VB
                        > parlance).[/color]

                        Nothing is somewhat overloaded in VB.NET - it means the default value
                        for any type, so null, 0, false, '\0' or a struct with every field set
                        to the default value for its type.

                        --
                        Jon Skeet - <skeet@pobox.co m>
                        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                        If replying to the group, please do not mail me too

                        Comment

                        • cody

                          #13
                          Re: null == int testing

                          I have to wonder why the default implementation doesn't do a bitwise binary
                          comparison of the struct like C's memcmp()? For reference types in the
                          default implementation only the reference is tested for bitwise equality,
                          and not the reflected fields are compared using Equals().


                          "Jon Skeet [C# MVP]" <skeet@pobox.co m> schrieb im Newsbeitrag
                          news:MPG.1d15e7 834ac4315798c2d 5@msnews.micros oft.com...[color=blue]
                          > <"Sherif El-Metainy" <elmeteny REMOVETHIS at thewayout NOSPAM dot
                          > net>> wrote:[color=green]
                          >> Then we can use Object.Equals, which will suffer performance penalties
                          >> becaue of boxing[/color]
                          >
                          > Using Object.Equals doesn't so much suffer performance penalties
                          > because of boxing, but because the implementation of ValueType.Equal s
                          > uses reflection to compare the value of each field in turn.
                          >
                          > --
                          > Jon Skeet - <skeet@pobox.co m>
                          > http://www.pobox.com/~skeet
                          > If replying to the group, please do not mail me too[/color]


                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: null == int testing

                            cody <deutronium@gmx .de> wrote:[color=blue]
                            > I have to wonder why the default implementation doesn't do a bitwise binary
                            > comparison of the struct like C's memcmp()? For reference types in the
                            > default implementation only the reference is tested for bitwise equality,
                            > and not the reflected fields are compared using Equals().[/color]

                            I think it does a bitwise binary comparison in some cases, but uses
                            Equals() to compare fields if any of the fields override Equals - so if
                            your struct contains a string reference, two distinct references to
                            equal strings in two instances of the struct won't stop those instances
                            from being equal.

                            --
                            Jon Skeet - <skeet@pobox.co m>
                            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                            If replying to the group, please do not mail me too

                            Comment

                            • cmay

                              #15
                              Re: null == int testing

                              Thanks everyone.

                              I was sure that I had tried:
                              if (new System.Drawing. Color() == MyColor)

                              But I must not have because it works like I wanted it to.

                              Thanks!

                              Comment

                              Working...