Tuesday, January 26, 2016

Lesson 3

Outline


Chapter 10

Properties
     Full
     Private Fields
     Auto-Implemented
     Read Only
     Write Only
     Calculated

Properties

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

Properties are used in classes to enable the derived objects to hold values (even different values) within the object. Properties generally are associated with a private field variable that is not accessible outside the class/object. The property provides a method to set or get the value of the private field from outside the object.

It is important to think of properties as adjectives or descriptors of the object. Suppose we had a person class. Some properties might be used to describe the object (class instance)  such as name, height, weight, race, or gender. The person class containing these properties allow the object to maintain its own unique description. So a person object for me might have different values for each of the properties even though they are based on the same class.

Full Property

Full properties allow the greatest flexibility for a developer. All of the parts of the property are exposed and can be modified to create the desired behavior. Below we have a simple person class with a single property Name.

class Person
{
 
    private string name;

     public string Name
     {    
     get { return name; }
     set { name = value; }
     }  
}
 


Examining the property we see that part of the property is public and part is private. The public Name is what the developer will see when the property is accessed from an object. The private name will hold the value set by developer.
Person me = new Person();
me.Name = "Larry Taylor";

In the code above we first create an instance of the Person class, in this case our object will be named me. (Note: me is a horrible object name, please try to use a descriptive name for your instance). Once me is instantiated we can then access the properties defined in the class via the newly created object. me.Name is accessing the public Name property we defined in the class allowing me to set the value of the private name variable to "Larry Taylor"
When a value is applied to a property it accesses the properties "set" statement. In our name property we defined we have this statement: set { name = value; } When value the is applied the property sets the private field "name" equal to the value being passed into the property.
The reverse is true as well. When the get accessor is called the value stored in the private field is returned.

Console.WriteLine(me.Name);

Name refers to the property and within the property the private field value is returned.

Auto-Implemented

Sometimes (well most of the time) your class will only need simple properties. C# has a shortcut for creating simple properties call auto-implemented properties. These properties work EXACTLY like the full properties above have a lot of the plumbing hidden from the developer. The Name property we created above using the full property approach would be written as follows using auto-implemented:
       
public string Name { get; set; }

We still have a get and set to apply and retrieve the value held in the private field. But where is the private field? It is still there only hidden. This approach makes for a much simpler property to generate within the class. The shortcut for the auto-implemented property is prop followed by two tabs.

Read Only Properties

Read only properties cannot have the private value set outside of the class. We can still read it from outside the class but not change it. Think about a class for account management at a bank. We might want to set the account number once from within the class and then all the methods within the class access the account number during the debit and credit functions of the account management process. We would not want the account number to be changed mid process when making a deposit.

Read Only Full Property

     private string name;

     public string Name
     {    
     get { return name; }
     } 

Note: The set accessor is no longer present.

Read Only Auto-Implemented Property

public string Name { get; private set; }

The set accessor is marked private to prevent access from outside the class

The value of the property can only be set within the constructor when we instantiate a class into an object or within a method located in the same class. 

Write Only Property

These are properties that can be set outside the class but not read from outside the class. This type of property is not used that often. Examples are below:

Read Only Full Property

     private string name;

     public string Name
     {    
     set { name = value; }
     }  
 
Read Only Auto-Implemented Property

public string Name { private get; set; }

Properties are a useful way to enable your class/object to hold data specific to that instance of the object.

Calculated Properties

This type of property "assembles" or generates the return value of the property dynamically when the get accessor is called.

public string FirstName { get; set; }
public string LastName { get; set; }

public string FullName
{get { return FirstName + ' ' + LastName; }
} 
Our example above contains 3 properties. Two of the properties allow the developer to read and write the first and last name. The third property is a read only property that assembles the first and last name property values into a full name property. Since we have the first and last name there is no need to create a property to accept a full name. We just use the values we already have in the class.

Properties are an essential component of object oriented programming. They allow our objects to retain values specific to its instance and allow for proper encapsulation of information.


No comments:

Post a Comment