Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Structures in the .NET framework have an IsEmpty method (Rectangle.IsEmpty for example). Does anyone know how these methods are implemented?
Gamer extraordinaire. Programmer wannabe.
Posted

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.

Gamer extraordinaire. Programmer wannabe.
  • *Gurus*
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...