The World According to Nick
My take on Software, Technology, Politics, and anything else I feel like talking about.
Tuesday, March 02, 2004

Is a Property a Field or Function? 

I've been reading a book called The Elegant Universe lately... talking about fun things like String Theory, Hidden Dimensions, Theory of Everything... etc. You know light reading. One of the things being discussed in the book right now is the dual nature of light. The fact that it acts as both a wave and a particle. What does this have to do with properties in .NET you ask? Like light, properties are a funky little thing. They're syntacticly like a field. But their underlying implementation is a pair of functions. In essense, a property is a field where the programmer is allowed to do extra type checking, and validation... abstracting away those details so the consumer of the property doesn't even need to know thats happening. A programmer could even create a property called Age, but never store the age in the object. Instead, he might track the birth date, and return the age each time by doing substraction with the current date. You as the property consumer don't need to know. So should we even be aware that properties are actually a pair of get_ and set_ functions under the hood? Take the following example:

class MyClass
{
   public int MyProperty
   {
      get { /* ... */ }
      set { /* ... */ }
   }

   public int MyField;

   public void MyMethod( ref int n )
   { /* Do something with n */ }

   public void MyTest()
   {
      MyMethod( ref MyField ); // Works fine
      MyMethod( ref MyProperty ); // Uh oh!
   }
}

If properties were really properly abstracted, you should be able to pass it as ref parameter to a method. But you can't. You will get a nice little compiler error saying "A property or indexer may not be passed as an out or ref parameter". Instead you'd have to implement MyTest like this:

public void MyTest()
{
   MyMethod( ref MyField ); // Still fine
   int temp = 0;
   MyMethod( ref temp );
   MyProperty = temp;
}

Wouldn't it be nice if the C# compiler would generate that extra boilerplate code in MSIL for you... thus keeping the abstraction... and never really revealing the fact that a property is really a pair of methods? Just a thought. Of couse if you know that a property is really two methods... you can take advantage of it if you want.

About Me



Name: Nick
Home: Wauwatosa, WI, United States

I'm a Software Consultant in the Milwaukee area. Among various geeky pursuits, I'm also an amateur triathlete, and enjoy rock climbing. I also like to think I'm a political pundit.


 View My Profile

Archives
 Home Page

Subscribe to this Feed

Search Archives
Previous Posts
How Fast is Fast Enough?
Something of Note with the New Iraqi Constitution
The Passion of The Christ Reviews
Kerry's Haiti jab
Wrapping Up Properties with Delegates
CodeGuru has a New Look
Rethinking Free Software
Redefining Low and High when it comes to Unemployment
They Are Illegal... and They Are Aliens
Why Isn't Anyone Asking Her to Resign?

Personal Links
Carnival of the Badger
The Coding Monkey
del.icio.us Links
Flickr Photos
Blog Critics Reviews





Blogroll Me!

music
books
video
culture
politics
sports
gaming

www.flickr.com
This is a Flickr badge showing public photos from Nick_Schweitzer. Make your own badge here.

Credits

Blogcritics: news and reviews







This page is powered by Blogger. Isn't yours?

Weblog Commenting and Trackback by HaloScan.com

RSS-to-JavaScript.com

Listed on BlogShares

Design By maystar