Anda di halaman 1dari 4

Auto-Implemented Properties

(a.k.a Automatic Properties)


Note: This is the first part of my C# 3.0 tutorial series.
Auto-implemented properties is the C# 3.0 new feature that make
property-declaration more concise. It helps you to save some of your
time for typing a lot of codes. Please take a look the following code to
know how it looks like.
1
2
3
4

class Car
{
public string Speed { get; set; }
}

Why Auto-Implemented
Properties?
Looking back the way that we have been doing for creating a property
in so many projects, do you notice that you have been writing so
many line codes just for creating a simple property? Look at the code
below.
1
2
3
4
5
6
7
8
9
10
11
12
13

public class Employee {


private int _id;
private string _firstName;
private string _lastName;
public int ID{
get{
return _id;
}
}
public string FirstName {
get {
return _firstName;
}
set {

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

_firstName = value;
}
}
public string LastName {
get {
return _lastName;
}
set {
_lastName = value;
}
}
}

Code 1.0: Employee class that doesnt use auto-implemented


properties
This is what we used to create the property in C#. In order to create
one simple property, we have to type extra four or five lines of code.
So, if you have one hundred properties in different entity classes, you
have to type toooo much code just for creating properties.
I think VS IDE team at Microsoft also awared of this situration.
Thats why when they released Visual Studio 2005 with .NET
framework 2.0, we got the better way to deal with creating properties.
You can simply type prop and press tab key twice. then, VS IDE
will generate the property for you. Or you can use Encapsulate
Field from Refactor menu to create the property. Unfortunately,
those features are available only in C# project type in VS 2005. If you
are using VB.NET project, you wont be able to use that feature.
And when Microsoft shipped .NET framework 3.0, the new feature
called Auto-Implemented Properties (a.k.a Automatic Properties) is
introduced in Framework 3. It helps you to save some of your typing

time for declaring private variable, setting the value to that private
variable and returning the value.
Take a look at the class that I have converted from normal class (Code
1.0) to the class with auto-implemented properties. Its much shorter,
right?
1
2
3
4
5
6

class Employee
{
public int ID{ get; private set; } // read-only
public string FirstName { get; set; }
public int LastName { get; set; }
}

Code 1.1: Employee class that uses auto-implemented properties

Conclusion
Auto-Implemented Properties is one of the most popular features of
C# 3.0 and a lot of developers are really happy with that new feature.
but you should know that auto-implemented properties are nothing
new except it helps you to boost your productivity.

FAQs
1. Can Auto-implemented Properties improve the performace?
No. It is just helping you to type shorter code but it doesnt improve
the performance since the compiler will generate the same as the way
what it generates for normal class that doenst have auto-implemented
properties.
2. How can I make read-only or write-only auto-implemented
properties?

You can make the accessor as private. For example: If you want to
make read-only property then You can put private in setter. You
can read more about that in C# 3.0 specification.
It mentioned like below in C# 3.0 specification ~
When a property is specified as an automatically implemented
property, a hidden backing field is automatically available for the
property, and the accessors are implemented to read from and write to
that backing field.
Because the backing field is inaccessible, it can be read and written
only through the property accessors. This means that automatically
implemented read-only or write-only properties do not make sense,
and are disallowed. It is however possible to set the access level of
each accessor differently. Thus, the effect of a read-only property
with a private backing field can be mimicked like this:
1
2
3
4
5

public
public
public
public
}

class ReadOnlyPoint {
int X { get; private set; }
int Y { get; private set; }
ReadOnlyPoint(int x, int y) { X = x; Y = y; }

Anda mungkin juga menyukai