Properties

 
Chapter 3 - Object-Oriented C#
bySimon Robinsonet al.
Wrox Press 2002
  

Properties are unusual in that they represent an idea that C# has taken from VB, not from C++/Java. The idea of a property is that it is a method or pair of methods that are dressed to look like a field as far as any client code is concerned . A good example of this is the Height property of a Windows Form. Suppose you have the following code.

   // mainForm is of class System.Windows.Form     mainForm.Height = 400;   

On executing this code, the height of the window will be set to 400 and you will see the window resize on the screen. Syntactically, the above code looks like we're setting a field, but in fact we are calling a property accessor that contains code to resize the form.

In this section we assume you are familiar with the concept and use of properties. If you are unsure about them, Appendix A of this book contains a section that explains the concept in detail.

To define a property in C#, we use the following syntax.

   public string SomeProperty     {     get     {     return "This is the property value";     }     set     {     // do whatever needs to be done to set the property     }     }   

The usual restrictions apply. The get accessor takes no parameters and must return the same type as the property has been declared as. You should not specify any explicit parameters for the set accessor either, but the compiler assumes it takes one parameter, which is of the same type again, and which is referred to as value . As an example, the following code contains a property called ForeName , which sets a field called foreName , which has a length limit.

   private string foreName;     public string ForeName     {     get     {     return foreName;     }     set     {     if (value.Length > 20)     // code here to take error recovery action     // (eg. throw an exception)     else     foreName = value;     }     }   

Note the pattern of naming here. We take advantage of C#'s case sensitivity by using the same name , Pascal-cased for the public property and camel-cased for the equivalent private field if there is one. This is standard practice.

VB 6 programmers should note that the syntax for defining properties is very different in C#. Whereas in VB 6, Get and Set accessors are defined as separate functions, in C# they are placed together inside a single property declaration. In VB you also explicitly declare the parameter to the set accessor and can therefore choose its name, whereas in C# this parameter is implicitly assumed and always named value . Recall also that C# does not distinguish between VB Set and VB Let : in C#, the write accessor is always termed set , no matter what the property type.

Read-Only and Write-Only Properties

It is possible to create a read-only property by simply omitting the set accessor from the property definition. Thus, to make ForeName read-only in the above example:

 private string foreName;   public string ForeName     {     get     {     return foreName;     }     }   

It is similarly possible to create a write-only property by omitting the get accessor. However this is regarded as poor programming practice because it could be confusing to authors of client code. In general, it is recommended that if you are tempted to do this, you should use a method instead.

Access Modifiers

C# does not permit setting different access modifiers to the get and set accessor. This may affect you if you have a property that you need public access for reading, but where you want to restrict write access to derived classes. If you need this feature then you'll need to use some workaround, such as declaring a public read-only property and a protected Set() function.

 public string ForeName {    get    {       return foreName;    } }   protected void SetForeName(string value)     {     if (value.Length > 20)     // code here to take error recovery action     // (eg. throw an exception)     else     foreName = value;     }   

Virtual and Abstract Properties

It is also permitted to declare a property as virtual or abstract. For a virtual or overridden property, the syntax is the same as for a non-virtual property except for the addition of virtual or override in the definition, as for methods. For an abstract property, the syntax looks like this:

   public abstract string ForeName     {     get;     set;     }   
  


Professional C#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

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