20.1 Properties as a replacement for accessor and mutator methods


20.1 Properties as a replacement for accessor and mutator methods

In C#, instead of writing accessor and mutator methods, you can use a special class member called a property. I have altered the class above to replace the accessor/mutator methods with a public property called Color . This property represents the private field MyColor . Study the code below.

 1: using System;  2:  3: public class TestClass{  4:   private string MyColor = "yellow";  5:  6:   // property Color  7:  public string Color{  8:  get{  9:  return MyColor;  10:  }  11:  set{  12:  MyColor  =  value;  13:  }  14:  }  15:   public static void Main(){ 16:     TestClass c = new TestClass(); 17:     Console.WriteLine(  c.Color  ); // get 18:  c.Color  =  "blue";  // set 19:     Console.WriteLine(c.Color); // get 20:   } 21: } 

Output:

 c:\expt>test yellow blue 

Notice the special method-like construct called Color (lines 7 “ 14) which is declared public like most accessor/mutator methods. Color contains a 'get section' [4] and a 'set section' which corresponds to the GetMyColor() and SetMyColor() methods which you would normally have written.

[4] Note that 'get' and 'set' are not C# keywords but have special meanings when used in properties.

When retrieving the value of MyColor , you can simply use the Color property, as in c.Color where c is the variable that refers to the instance with that property (lines 17 and 19). When setting the value of MyColor , just assign c.Color to the new value (line 18). The appropriate sections of code (the get and set sections) will execute.

A note about the value [5] variable used in the set section (on line 12). value is an implicit field (meaning it is not explicitly declared or initialized ) that is available for the developer's use in the set section only. value refers to the value that has been assigned by the calling statement (in this example, value is blue because of line 18).

[5] 'Value' is also not a C# keyword.

In the example above, I have used the Color property to allow external parties to access and change the private field MyColor . It is usual for C# developers to use the same identifier for the private field and the public property it represents. [6] Of course, the identifiers must have different casings. By convention, if you choose to give the same identifier to a public property and the private field it represents, you can choose to name the private field using camel casing (starting with a lower case letter) and the public property using Pascal casing (starting with an upper case letter). For this example, it is generally acceptable to call the private field myColor , and the public property MyColor .

[6] The reason I selected a different identifier for this chapter was to reduce the confusion that might be caused.

It should be obvious that a public property can be used just like a public field outside the class, and that's the cool thing about properties. You maintain absolute control of what you want to happen within the property declaration when the property is assigned a new value, or when a value is obtained via the property. It is unlike a real public field in which there is no way you can control what values are assigned to it. [7]

[7] You can perform actions such as checking for valid values in the property's set section before actually assigning the new value to the private field.

Another nice about using properties is that, like accessor and mutator methods, you can only have either a get section or a set section. It is not mandatory to have both sections within a property declaration. And this, again, means that, unlike a public field, you can allow external parties to access the value of the property (by providing only a get section), but not change it (don't provide a set section).



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net