wyrd Posted July 31, 2003 Posted July 31, 2003 Structures in the .NET framework have an IsEmpty method (Rectangle.IsEmpty for example). Does anyone know how these methods are implemented? Quote Gamer extraordinaire. Programmer wannabe.
*Gurus* divil Posted July 31, 2003 *Gurus* Posted July 31, 2003 In the case of the Rectangle, it just checks to see if every value (top, left, width and height) is zero. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
wyrd Posted July 31, 2003 Author Posted July 31, 2003 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. Quote Gamer extraordinaire. Programmer wannabe.
*Gurus* Derek Stone Posted July 31, 2003 *Gurus* Posted July 31, 2003 Declare the object as follows: 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: 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. Quote Posting Guidelines
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.