I try thisThis fails, so I say boxing is a flaw. If not, then how can I check for a null struct please?PHP Code:StructX x=func();
while(null!=(object)x)
{
//do something
x=anotherfunc();
}
Thanks
Printable View
I try thisThis fails, so I say boxing is a flaw. If not, then how can I check for a null struct please?PHP Code:StructX x=func();
while(null!=(object)x)
{
//do something
x=anotherfunc();
}
Thanks
Structs are Valutypes, use CLASSES !!
:D
Another way would be using nullable Types.
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 --> classesCode: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;
}
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.