1. Computing

C# Tutorial - An Object Oriented Approach to Programming

By

5 of 10

About Pascal and Camel Case
Property Syntax It's customary (according to Microsoft's Design Guidelines to use Camel case names for fields and Pascal case for properties. Pascal case means that the first letter in each word in an identifier is capitalized, camel case is similar except the first letter is in lower case.
  • Pascal case: GreenButtonType.
  • Camel case: myInt
As a general rule Pascal case is used everywhere except for parameters and private or protected fields which use Camel case. This is why length is the field but Length is the property.

This is a general example of a property.

 private int propertyName public int PropertyName {
    get { return propertyName;}
    set { propertyName=value;}
 }
 
This is the minimum code you need to implement for both readable and writeable properties. The identifier value is provided by the compiler to represent the value that the property is assigned. You can add more code within the get or set functions. A common use is to limit the range of values.
 public int Speed
 {
     get { return speed; }
     set { 
         if (speed <0 || speed > 70)
           throw new Exception("Speed outside range 0..70") ;
         speed = value; 
     }
 }
 
Here an exception is raised if a property is assigned with a value outside the acceptable range. You could just coerce the speed within this range but raising an exception like this tells you the developer that something went wrong in the calling code and helps you fix the code before you ship it. We'll cover exceptions in more detail in a future tutorial.

On the next page : About Object Methods and Constructors

  1. About.com
  2. Computing
  3. C / C++ / C#
  4. C# / C Sharp
  5. Learn C Sharp
  6. About Pascal and Camel Case

©2014 About.com. All rights reserved.