Auto Implemented Properties by ThinqLinq

Auto Implemented Properties

I was looking over the help documentation included as part of the March Orcas CTP which I recently downloaded. I noticed that most of the documentation that came with the previous previews appears to have not made it into this release yet. If you want documentation and samples telling you what LINQ is all about, I still recommend grabbing the MAY CTP which you can find from my LINQ Links post a while back. If you prefer, soon you will be able to catch a book on LINQ I'm helping out on. Check out for updates.

I did notice one new feature I don't recall seeing elsewhere before in the C# LINQ overview offered in the integrated Orcas Help file called Auto-Implemented Properties. With Auto-Implemented Properties, you can create a private field and public property accessor with a single line of C# code like this:

public string Test { get; set; }

Typically when working in OOP environments, we have to choose between structures and classes to represent our data in memory. Structures have an advantage in that they require a bit less memory overhead and are more consise to type. However they suffer in their implementation due to issues with instantiation (which typically offsets the memory overhead) and marshalling. Structures are value types which are passed byRef where-as with objects, the pointer to the object is passed rather than the contents. Many people are caught offguard when passing a structure expecting the values to be updated and finding that they aren't. Objects typically take a bit more effort to type (private fields, public accessors, etc) and are slightly slower to instantiate. The benifits of classes easily outweigh those of structures in most cases.

C# 3.0 offers a new way to declare simple properties in classes to make them nearly as easy to create as properites in structures. Consider the following existing C# code snippet:

private string _Test1;
public string Test1
get { return _Test1; }
set { _Test1 = value; }

Here, we are creating a simple private field and exposing a public accessor for it. We aren't adding any business logic to the property, rather implementing a simple object persistance mecanism. In C# 3.5, we will now be able to replace the above code with a one-line statement:

public string Test { get; set }

Thanks to Lutz Roder's recent release of Reflector, we can now look under the covers of .Net 3.5 code. Here is what the compiler translates the one line of code above into:

// Fields
private string <>k__AutomaticallyGeneratedPropertyField0;

// Properties
public string Test
return this.<>k__AutomaticallyGeneratedPropertyField0;
this.<>k__AutomaticallyGeneratedPropertyField0 = value;

In essense, the compiler is doing all of the hard work for use. It creates the expected accessors and adds an anonymous type for the private field, in this case called <>k__AutomaticallyGeneratedPropertyField0. Unfortunately, if we want to include any business logic, including change managment (implementing IChangeTracking or other such interfaces), we will need to do that ourself as the property setter does not include the code necessary to handle those notifications.

Posted on - Comment
Categories: C#) -
comments powered by Disqus