Click to See Complete Forum and Search --> : boxing


Khiem
October 20th, 2008, 04:09 AM
I try this
StructX x=func();
while(null!=(object)x)
{
//do something
x=anotherfunc();
}

This fails, so I say boxing is a flaw. If not, then how can I check for a null struct please?
Thanks

JonnyPoet
October 20th, 2008, 05:19 AM
Structs are Valutypes, use CLASSES !!

:D
Another way would be using nullable Types.
MyStruct? xy = new MyStruct();
if (xy != null) {
Console.WriteLine("Struct is created");
MyStruct stest = xy.Value;
stest.Val = 5;
stest.Name = "Hallo";
xy = new Nullable<MyStruct>(stest);
}

public struct MyStruct {
public int Val;
public string Name;
}


As you see handling a simple struct to use it as nullable is a bit a strange task. If you need objects use referene types --> classes

BigEd781
October 20th, 2008, 03:20 PM
Yeah, you are guaranteed that a value type will not be null by default. I would not start making nullable structs unless you have a very good reason for it. This is definitely not a language flaw.