C# 3.0 Features: Automatically Implemented Properties
C# 3.0 is the new version of the language includes many new features inspired by functional programming language, to not be confused the 3.0 means the version of the language specification and not the .NET Framework version.
I will talk here about the Auto implemented properties, and I want to mention here that it's not an enhancement on the language that’s reflect an enhancement on the generated intermediate language (IL); it's just a new compiler feature or we can say it’s a kind of productivity enhancement and can replace one of the code generation tools features.
It's cool to have a new quick way to add new properties without the need to write the getter and setter declarations.
Every developer has written entity or model class before, these classes which contains the trivial properties with both getter and setter for each fields.
The classic way of adding properties is like the following simple example:
|
namespace Properties.Classic
{
/// <summary>
/// This is the classical class of JordevMember using trivial way of implementing properties.
/// </summary>
public class JordevMember
{
private string _name;
private string _job;
private string _email;
private DateTime _dateofbirth;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string Job
{
get
{
return _job;
}
set
{
_job = value;
}
}
public string Email
{
get
{
return _email;
}
set
{
_email = value;
}
}
public DateTime DateOfBirth
{
get
{
return _dateofbirth;
}
set
{
_dateofbirth = value;
}
}
}
} |
The Automatic Properties delegates the writing of the getter and setter for the private fields and generates backing fields for this target.
Below the JordevMember class implemented using the Automatic Properties:
|
namespace Properties.AutoImplemented
{
public class JordevMember
{
public string Name { get; set; }
public string Job { get; set; }
public string Email { get; set; }
public DateTime DateOfBirth { get; set; }
}
} |
Let's go internally and check out how the compiler treats the previous classes in the classic and the new version.
The easiest way to slice the properties is to disassemble it using the ILDASM utility, Here's the IL generated by the C# compiler for the getter accessor of the Name property in the classic version of Jordev Member:
And here's the generated IL of the new version of JordevMember with the automatically implemented properties:
And now we will find that the C# compiler generates the backing field which named <Name>k__BackingField this field holds the data of the property like the private fields we defined above. The compilers architect pay attention for something very important which is the naming style of the auto-generated properties because the '<Name>' prefix is not allowed to be used in any C# identifier.
If you need to have a read-only property you can use the access modifiers with the getter and setter like the following:
|
//Read Only Property
public string Name { get; private set; }
//Write Only Property
public string Job { private get; set; } |
Simply the automatic implemented properties are the quickest way to write the trivial properties of the classes.
Cheers,