Null structs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • S. Lorétan

    Null structs

    Hello.

    I have some structs in different namespaces/classes/other structs and I
    sometime have to check if it contains something or not.

    myStruct == null doesn't work.

    I've currently done it by creating a IsNull method in my structs:

    struct TestStruct {
    public int Value;

    public bool IsNull() {
    return this.Equals(new TestStruct());
    }
    }

    Is this correct? Is there another way?
    --
    S. Lorétan <http://www.tynril.info/(This link is in french. Sorry.)


  • Mathieu Cartoixa

    #2
    Re: Null structs

    S. Lorétan a écrit :
    I have some structs in different namespaces/classes/other structs and I
    sometime have to check if it contains something or not.
    >
    myStruct == null doesn't work.
    Value types (structs) cannot be null, they are always instanciated. By
    the way, you are not checking if your instance contains something, you
    are checking if your instance is ... instanciated.
    I've currently done it by creating a IsNull method in my structs:
    >
    struct TestStruct {
    public int Value;
    >
    public bool IsNull() {
    return this.Equals(new TestStruct());
    }
    }
    >
    Is this correct? Is there another way?
    As for this example, new TestStruct() creates an instance of TestStruct
    with all its fields initialized with their default type value. For an
    int (which is a value type), that is 0.
    The default implementation of Equals for a value type uses reflection to
    check fields equality. So in this case, IsNull will return true if Value
    equals 0. I cannot assert the correctness of this behaviour.

    Another way ? If your classes may not be instanciated, use reference
    types (classes) instead of value types. In .NET 2.0, you could also use
    the Nullable<Tclass .
    Or have your value type manage its null state. I would have it this way :

    interface INullable {
    bool IsNull {
    get;
    }
    }

    struct TestStruct: INullable {
    private int _Value;
    private bool _IsNull;

    public TestStruct(int value) {
    Value=value;
    }

    public int Value {
    get {
    if (_IsNull)
    throw new InvalidOperatio nException();
    return _Value;
    }
    set {
    _Value=value;
    _IsNull=false;
    }
    }

    public bool IsNull {
    get {
    return !_IsNull;
    }
    }
    }


    Mathieu

    Comment

    • Stoitcho Goutsev \(100\)

      #3
      Re: Null structs

      You can use the new c#2 sintax for nullable value types.
      Forexample if you have structure
      struct Foo
      {
      }

      you can declare variable such as

      Foo? f = null
      or
      Foo? f = new Foo();

      the you can test
      if(f == null)
      {
      }

      or
      you can use

      if(f.HasValue)
      {
      }

      The actuall value type is exposed via the Value property

      f.Value returns the actuall value if the HasValue is true.

      you can also use casting

      Foo foo = (Foo)f;

      Keep in mind that the Value property or casting return copy of the value, so
      changing properties won't change the original value.
      C# also has one new operator related to nullable value types - the ??
      operator that can provide deault value if the nullable value type is null.
      If you want to use this new feature of the language I'd suggest reading in
      MSDN about it since it has some details related to arithmetic and
      comparision operations when one of the operands is null. Just go in the MSDN
      search tool and use "nullbale types" as a keyword.


      --
      HTH
      Stoitcho Goutsev (100)
      "Mathieu Cartoixa" <mathieu.cartoi xa@_NO_hotmail_ SPAM_.comwrote in message
      news:4520ddee$0 $3254$636a55ce@ news.free.fr...
      S. Lorétan a écrit :
      >I have some structs in different namespaces/classes/other structs and I
      >sometime have to check if it contains something or not.
      >>
      >myStruct == null doesn't work.
      Value types (structs) cannot be null, they are always instanciated. By the
      way, you are not checking if your instance contains something, you are
      checking if your instance is ... instanciated.
      >
      >I've currently done it by creating a IsNull method in my structs:
      >>
      >struct TestStruct {
      > public int Value;
      >>
      > public bool IsNull() {
      > return this.Equals(new TestStruct());
      > }
      >}
      >>
      >Is this correct? Is there another way?
      As for this example, new TestStruct() creates an instance of TestStruct
      with all its fields initialized with their default type value. For an int
      (which is a value type), that is 0.
      The default implementation of Equals for a value type uses reflection to
      check fields equality. So in this case, IsNull will return true if Value
      equals 0. I cannot assert the correctness of this behaviour.
      >
      Another way ? If your classes may not be instanciated, use reference types
      (classes) instead of value types. In .NET 2.0, you could also use the
      Nullable<Tclass .
      Or have your value type manage its null state. I would have it this way :
      >
      interface INullable {
      bool IsNull {
      get;
      }
      }
      >
      struct TestStruct: INullable {
      private int _Value;
      private bool _IsNull;
      >
      public TestStruct(int value) {
      Value=value;
      }
      >
      public int Value {
      get {
      if (_IsNull)
      throw new InvalidOperatio nException();
      return _Value;
      }
      set {
      _Value=value;
      _IsNull=false;
      }
      }
      >
      public bool IsNull {
      get {
      return !_IsNull;
      }
      }
      }
      >
      >
      Mathieu

      Comment

      Working...