Auto Impl Property

3

Click here to load reader

Transcript of Auto Impl Property

Page 1: Auto Impl Property

8/9/2019 Auto Impl Property

http://slidepdf.com/reader/full/auto-impl-property 1/3

http://www.blackwasp.co.uk/CSharpAutomaticProperties.aspx

C# Automatically Implemented Properties by Richard Carr , published at http://www.blackwasp.co.uk/CSharpAutomaticProperties.aspx

Often a class may contain properties that provide no behaviour other that permitting the storage of values. This can lead to many space-consuminglines of code for get and set accessors. Using .NET 3.0, these can bereplaced ith automatic properties.

 A Simple PropertyClass properties  are used to provide the publicly visible aspects o state o anob!ect. "ten a property perorms some calculation or lookup beorereturnin this state. $owever% in many cases the property simply holds avalue that can be manipulated or read by other   classes .&o create such a property usin the normal% lonhand method% threeelements are re'uired. &he irst is the property declaration% whichdetermines the type and the name o the property. &he second itemsre'uired are the accessors. &hese allow the data in the property to be reador written to% and determine whether the property is ully accessible% read(only or write(only. )inally% a backin store ield is re'uired. &his is thevariable that holds the property*s value and is usually declared as eitherprivate or protected.&he ollowin code declares a class with a sinle property that contains allo the above elements:class Employee{  private string _name; // Backing Store

 public string Name // Property Declaration

  {

  get  // Get Accessor  {  return _name; 

  set  // Set Accessor  {  _name ! value;   

&his syntax or declarin a read/write property includes a lare number o

lines o code or a relatively simple task. +sin the layout and ormattinshown% a sinle property involves twelve lines. )or a class containin !ust

Page 2: Auto Impl Property

8/9/2019 Auto Impl Property

http://slidepdf.com/reader/full/auto-impl-property 2/3

ten properties% one hundred and twenty lines would be re'uired% limitin theability to easily read and comprehend the class*s public interace.

 Automatically Implemented Properties !utomatic properties were introduced in C# in the .,-& ramework version

.. &he external behaviour o automatic properties is identical to that o thesimple properties explained above. &hey provide state that can bemanipulated and read externally and permit the declaration o ullyaccessible% read(only or write(only properties.&he dierence between standard properties and automatically implementedproperties is in the syntax used or their declaration. Auto properties can bedeclared in a sinle statement. &his allows more compact and easy(to(understand prorams.&o create a simple% ully accessible property usin this alternative syntax%

the declaration remains the same but the accessors are created without acode block. &he ollowin class is the shorthand version o that seenpreviously.class Employee{  public string Name {get; set;

0ead("nly and 1rite("nly Automatic Properties1hen usin the shortcut syntax o automatic properties% both a et and setaccessor must be provided. 1ithout both accessors% the property would beuseless. $owever% it is possible to create properties that are either read(

only or write(only via the public interace whilst allowin the members o theclass to use the other accessor internally. &his is achieved by declarin oneo the accessors as private% or protected i you re'uire access in subclasses.&he ollowin sample shows a read(only and a write(only property:public int "ea#$nly { get; private set; public int %rite$nly { private get; set;

2rawbacks&here are some drawbacks to the use o automatic properties. &hese mustbe considered beore decidin which syntax to use. )irstly% there is noexplicit declaration o a backin store variable. &his is created automaticallyby the compiler but is not available or use by the class*s members. Anyaccess to the property value rom within its class must be via the propertyname itsel% with the option to use the 3this3 preix.

 Automatic properties can only be used in situations where simple storaeo a value is re'uired. ,o additional unctionality can be added to either othe accessors. &his has an additional drawback o not permittin validationto be carried out upon new property values in the set accessor.1ith standard property syntax a deault value can be applied by simplyassinin it to the backin store ield in its declaration. 2eault values

cannot be set or automatic properties. &o create an apparent deault valuethe property must be initialised in a constructor .

Page 3: Auto Impl Property

8/9/2019 Auto Impl Property

http://slidepdf.com/reader/full/auto-impl-property 3/3