c# properties a practical approach

5

Click here to load reader

Upload: wasiuddin

Post on 17-Feb-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C# Properties a Practical Approach

7/23/2019 C# Properties a Practical Approach

http://slidepdf.com/reader/full/c-properties-a-practical-approach 1/5

A Proposal for an Enhanced C# PropertysyntaxProposal to make C# properties exible whilst maintaining the Smart Field syntax.

ntrod!ction"icrosoft .E$ platform has pro%ided software programmers with a set of %ery powerf!llang!ages like &'( C# etc. and growing. $he syntax for writing code in these lang!ages issomewhat %ery similar since they all !se the "icrosoft C)* +Common )ang!age *!ntime,which forms the core of the .E$ platform.

"ost modern -bect -riented +--, lang!ages pro%ide s!pport for Primiti%e $ypes( Classes("ethods( -perators( Exception /andling( $hreading( E%ent /andling and Program FlowControl. 0hile working with C#( tho!ght that some feat!res of the lang!age co!ld ha%e beenimplemented a little di1erently to pro%ide more exibility.

 $his article( the 2rst amongst a series of articles will s!ggest enhancements in some of thefeat!re areas mentioned abo%e for the .E$ lang!ages. $he examples in this article arewritten !sing C# lang!age syntax ass!ming that the reader is familiar with the same.

0hat are C)* Properties3

 $he C)* !ses a new syntax for de2ning properties of a class. A C)* property is a member oftype that speci2es one or two methods that correspond to the named %al!e. $his syntaxmakes the property look as if it were a 2eld on that class +Also referred to as Smart Field,. $he

syntax is a hybrid of 2eld and method syntax or property is a cross between a logical 2eld andphysical method.

 $he str!ct!re of a C)* Property is as shown in the 2g!re abo%e. A property consists of amaxim!m of two accessor methods +get 4set , and it5s associated Type.

 $hePropertynfo class pro%ides access to property metadata and disco%ers the attrib!tes of aproperty. t pro%ides access to it5s !nderlying methods %ia calls tothe6etAccessors+, +ret!rns "ethodnfo obects for boththe set  and get  methods,( 6et6et"ethod+, +ret!rns the "ethodnfo obect for

Page 2: C# Properties a Practical Approach

7/23/2019 C# Properties a Practical Approach

http://slidepdf.com/reader/full/c-properties-a-practical-approach 2/5

the get  method, or6etSet"ethod+, +ret!rns the "ethodnfo obect for the set  method,methods. $he Property$ype property +no p!n intended, ret!rns the type of the property.

 // Current Permissible C# Property Syntax p!blic class Foo7  pri%ate string name8

  p!blic string ame  7  get 7 ret!rn this.name8 9  set 7 this.name : %al!e8 9  99

 $he abo%e property syntax res!lts in the generation of two methods which are marked witha specialname metadata attrib!te( which informs the compilers and tools +e.g. ntelliSense, tohide the properties indi%id!al methods from normal !se. n case yo! are wondering( this isprecisely why properties cannot be passed to methods !sing the ref  or out  keywords which ispossible if yo! !sed 2elds instead.

p!blic string get;ame+, // The getter.p!blic %oid set;ame+string %al!e, // The setter.

Analy<ing the Problem=!ite often we need a di1erent access scope for the property getter and setter for betterencaps!lation. $ypically( the public scope is assigned to the getter where as thelesser protected scope co!ld be applied to the setter. $his makes the setter only %isible to thes!b>classes and pre%ents any external mod!le or application from changing the state of o!robect !sing the otherwise public property setter.

Problem: $he c!rrent C)S +Common )ang!age Speci2cation, only allows !s to ha%e oneaccess ?!ali2er for e%ery property declared within a class. Assigning di1erent accessmodi2ers to the getter and setter of a property will res!lt in a compilation error. Consider themodi2ed code for a class Foo listed below@

p!blic class Foo7  pri%ate string name8  p!blic string ame  7  get 7 ret!rn this.name8 9  9

  // Compiler Error! The class Foo already contains a denition for "ame.  protected string ame  7  set 7 this.name : %al!e8 9  99

Workaround: e2ne a public read>only property in addition to a protected method+B, whichsets the %al!e on the property as shown below. $his negates the premise of making a propertylook like a smart 2eld.

Page 3: C# Properties a Practical Approach

7/23/2019 C# Properties a Practical Approach

http://slidepdf.com/reader/full/c-properties-a-practical-approach 3/5

pri%ate string name8

 // Current C# property syntax. Public getter for name.p!blic string ame7  get 7 ret!rn this.name8 99

 // Protected Set"ame method denition.protected %oid Setame+string name,7  this.name : name89

 // Somehere else in the code$ access the property of ob%ect type fooFoo foo : new Foo+,8string name : foo.ame8 // &' 

foo.Setame+myame,8 // "ot intuiti(e anymore.

Proposal: $he proposed syntax for de2ning a property is as shown below. -nly one getter

and setter irrespecti%e of the access scope sho!ld be permissible. $his new syntax alsoenables programmers to !se the (irtual keyword indi%id!ally for either the getter4setter orboth.

 // The desired syntax Public getter and protected setter.%irt!al p!blic string ame7  get 7 ret!rn this.name8 99

 // Protected setter access. Should compile &' as only one setter is denedprotected string ame7

  set 7 this.name : %al!e8 99

 $he abo%e property syntax wo!ld res!lt in the generation of two methods as before b!t withthe appropriate modi2cations to the access and %irt!al ?!ali2ers.

%irt!al p!blic string get;ame+, // The getter. "otice the (irtual 'eyord and public access modier.protected %oid set;ame+string %al!e, // The setter. "otice the protected access modier.

 $he following code with the new proposed syntax sho!ld res!lt in a compiler error as only onegetter and setter is permitted per property.

p!blic string ame7  get 7 ret!rn this.name8 9  set 7 this.name : %al!e8 99 // Error )uplicate denition of the "ame setter protected string ame7

Page 4: C# Properties a Practical Approach

7/23/2019 C# Properties a Practical Approach

http://slidepdf.com/reader/full/c-properties-a-practical-approach 4/5

  set 7 this.name : %al!e8 99

 // &*

p!blic string ame7  get 7 ret!rn this.name8 9  set 7 this.name : %al!e8 99 // Error )uplicate denition of the "ame setter protected string ame7  get 7 ret!rn this.name8 99

hope that C# properties in the f!t!re will become more friendlier if the C# comm!nity takesnotice. $il5 then /appy etting...

*eferences4)inksEssential .E$ &ol!me D +$he Common )ang!age *!ntime, > on 'ox with Chris Sells.

nside C# > $om Archer.

"icrosoft &is!al C# .E$ Step 'y Step > ohn Sharp( on agger.

Alternati%e Simpli2ed C# Property Syntax +Proposal, > Scott immerman article on C# Corner.

Page 5: C# Properties a Practical Approach

7/23/2019 C# Properties a Practical Approach

http://slidepdf.com/reader/full/c-properties-a-practical-approach 5/5