Auto Impl Properties
-
Upload
gingeevimal -
Category
Documents
-
view
213 -
download
0
Transcript of Auto Impl Properties
-
8/10/2019 Auto Impl Properties
1/4
-
8/10/2019 Auto Impl Properties
2/4
14
15
16
17
18
1920
21
22
23
24
25
26
27
28
29
_firstName = value;}}
publicstringLastName {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
-
8/10/2019 Auto Impl Properties
3/4
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 (Code1.0) to the class with auto-implemented properties. Its much shorter,
right?
1
2
3
4
5
6
classEmployee{publicintID{ get; privateset; } // read-onlypublicstringFirstName { get; set; }publicintLastName { 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 doensthave auto-implemented
properties.
2. How can I make read-only or write-only auto-implemented
properties?
-
8/10/2019 Auto Impl Properties
4/4
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
publicclassReadOnlyPoint {publicintX { get; privateset; }publicintY { get; privateset; }publicReadOnlyPoint(int x, inty) { X = x; Y = y; }}