inforakesha

A fine WordPress.com site

Automatic Properties,

Leave a comment

New C# Language Feature: Automatic Properties

If you are a C# developer today, you are probably quite used to writing classes with basic properties like the code-snippet below:

 

    public class Person {

        private string _firstName;
        private string 
_lastName;
        private int 
_age;
        
        public string 
FirstName {

            get {
                
return _firstName;
            
}
            
set {
                _firstName 
= value;
            
}
        }

        public string LastName {

            get {
                
return _lastName;
            
}
            
set {
                _lastName 
= value;
            
}
        }        
        
        
public int Age {

            get {
                
return _age;
            
}
            
set {
                _age 
= value;
            
}
        }
    }

 

Note about that we aren’t actually adding any logic in the getters/setters of our properties – instead we just get/set the value directly to a field.  This begs the question – then why not just use fields instead of properties?  Well – there are a lot of downsides to exposing public fields. Two of the big problems are: 1) you can’t easily databind against fields, and 2) if you expose public fields from your classes you can’t later change them to properties (for example: to add validation logic to the setters) without recompiling any assemblies compiled against the old class. 

The new C# compiler that ships in “Orcas” provides an elegant way to make your code more concise while still retaining the flexibility of properties using a new language feature called “automatic properties”.  Automatic properties allow you to avoid having to manually declare a private field and write the get/set logic — instead the compiler can automate creating the private field and the default get/set operations for you. 

For example, using automatic properties I can now re-write the code above to just be:

 

    public class Person {
    
        
public string FirstName {
            
get; set;
        
}

        public string LastName {
            
get; set;
        
}        
        
        
public int Age {
            
get; set;
        
}
    }

 

Or If I want to be really terse, I can collapse the whitespace even further like so:

 

    public class Person {
        
public string FirstName { get; set; }
        
public string LastName  { get; set; }        
        
public int    Age       { get; set; }
    }

 

When the C# “Orcas” compiler encounters an empty get/set property implementation like above, it will now automatically generate a private field for you within your class, and implement a public getter and setter property implementation to it.  The benefit of this is that from a type-contract perspective, the class looks exactly like it did with our first (more verbose) implementation above.  This means that — unlike public fields — I can in the future add validation logic within my property setter implementation without having to change any external component that references my class. 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s