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

wyrd

Well-known member
Joined
Aug 23, 2002
Messages
1,408
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 wasnt 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 doesnt allow access to unassigned local variables, even though those variables may be value types that dont 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:
Code:
Dim r As Rectangle
This will compile just fine, since the VB.NET compiler doesnt fuss about unassigned variables (which isnt 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.
 

Similar threads

J
Replies
0
Views
63
Jalil Sear [MCPD SharePoint]
J
J
Replies
0
Views
7
John Edwards, InformationWeek
J
E
Replies
0
Views
245
EN59CVH
E
Back
Top