Pages

Sunday, 14 June 2015

Properties

Properties

A property is a member that represents an item of data in a class or class instance. Using a property appears very much like writing to, or reading from, a field. The syntax is the same.

A property, like a field, has the following characteristics:
  • It is a named class member.
  • It has a type.
  • It can be assigned to and read from.
Unlike a field, however, a property is like a function member, hence:
  • It does not necessarily allocate memory for data storage.
  • It executes code.

Property Declarations and Accessors

A property is a named set of two matching methods called accessors. The accessors are get and set.
  • The set accessor always has the following:
    • A single, implicit value parameter named value, of the same type as the property
    • A return type of void
  • The get accessor always has the following:
    • No parameters
    • A return type of the same type as the property
Property has implicit parameter value in the set accessor. Like other value parameters, you can use it to send data into a method body—or in this case, the accessor block. Once inside the block, you can use value like a normal variable, including assigning values to it.

Use following syntax to create a property:
[Access Modifier : Optional][Type][Identifier]
{
 get
 {
  [statements to set property value]
 }
 set
 {
  [statements to get property value]
  return [property value];
 }
}

Take a look a following example:
class person
{
 private string realFirstName;
 private string realLastName;
 private int realAge;
 
 public string firstName
 {
  get { return realFirstName; }   
  set { realFirstName = value; }
 }

 public string lastName
 {
  get { return realLastName; }
  set { realLastName = value; }
 }

 public int age
 {
  get { return realAge; }
  set { realAge = value; }
 }
}

class Program
{
 static void Main(string[] args)
 {
  person me = new person();
  
  me.firstName = "Yang";    //set firstName property
  me.lastName = "Sopiana";   //set lastName property 
  me.age = 29;      //set age property

  Console.WriteLine(me.firstName + " " + me.lastName + " age " + me.age);  //access firstName, lastName and age property
  Console.Read();
 }
}

The code will produce:
Yang Sopiana age 29

Naming Conventions

There are several conventions used for naming properties and their backing fields.
  • Use the same string for both names but use camel casing for the field and Pascal casing for the property.
  • Use Pascal casing for the property, and then for the field, use the camel case version of the same identifier, with an underscore in front.
The following code shows both conventions:
private string firstName;    //camel casing
public string FirstName     //pascal casing
{
 get { return firstName; }
 set { firstName = value; }
}

private string _lastName;    //camel casing with leading underscore
public string lastName     //camel casing
{
 get { return _lastName; }
 set { _lastName = value; }
}

Performing Other Operations

Property accessors are not limited to just passing values back and forth from an associated backing field; the get and set accessors can perform any, or no, computations.

Lets review our age property in prior code. The age type is int, it can be possible for user to set age with negative value. Since there's no negative age, lets modify our code.
....
public int age
{
 get 
 {
  if (realAge < 0) //if value field value is negative, write something and return 17
  {
   Console.WriteLine("age is invalid, so computer will return default age");
   return 17;
  }
  return realAge;
 }
 set 
 {
  if (value < 0)  //if value is negative, write something
  {
   Console.WriteLine("invalid input, computer will return default value");
  }
  realAge = value; 
 }
}
....
class Program
{
 static void Main(string[] args)
 {
  person me = new person();
  
  me.firstName = "Yang";
  me.lastName = "Sopiana";
  me.age = 29;
  Console.WriteLine("First trial");
  Console.WriteLine(me.firstName + " " + me.lastName + " age " + me.age);
  
  Console.WriteLine("Second trial");
  me.age = -5;
  Console.WriteLine(me.firstName + " " + me.lastName + " age " + me.age);
  Console.Read();
 }
}

Current Code will display:
First trial
Yang Sopiana age 29
Second trial
invalid input, computer will return default value
age is invalid, so computer will return default age
Yang Sopiana age 17

0 comments:

Post a Comment