By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

Null structs

P: n/a
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.)
Oct 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 InvalidOperationException();
return _Value;
}
set {
_Value=value;
_IsNull=false;
}
}

public bool IsNull {
get {
return !_IsNull;
}
}
}
Mathieu
Oct 2 '06 #2

P: n/a
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.cartoixa@_NO_hotmail_SPAM_.comwrote in message
news:45**********************@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 InvalidOperationException();
return _Value;
}
set {
_Value=value;
_IsNull=false;
}
}

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

Oct 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.