A structs IsEmpty method.. how do you implement one?

wyrd

Senior Contributor
Joined
Aug 23, 2002
Messages
1,405
Location
California
Structures in the .NET framework have an IsEmpty method (Rectangle.IsEmpty for example). Does anyone know how these methods are implemented?
 
In the case of the Rectangle, it just checks to see if every value (top, left, width and height) is zero.
 
Hmm.. I see.

I thought it returned true if the structure wasn't initialized. You can declare a struct like so;

Rectangle rect;

In which case it throws an error if you try to use one of its properties. I thought the IsEmpty property checked for that, my mistake.
 
Declare the object as follows:
C#:
Rectangle rect = Rectangle.Empty;
This is a requirement of the C# compiler, since it doesn't allow access to unassigned local variables, even though those variables may be value types that don't need to be explicitly initialized. The proper handling of this compiler requirement is to initialize all variables, which in some cases I find to be rather annoying.

Take the following VB.NET code for example:
Visual Basic:
Dim r As Rectangle
This will compile just fine, since the VB.NET compiler doesn't fuss about unassigned variables (which isn't always a good thing mind you). Note that both lines of code generate the exact same line of MSIL and the C# error message is produced during compilation (as mentioned above), not at runtime.
 
Back
Top