Null structs

S

S. Lorétan

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?
 
M

Mathieu Cartoixa

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<T> class.
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
 
S

Stoitcho Goutsev \(100\)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top