developmentor - a developer services company				  
				      C# Tutorial
   Modules

Properties


Goals:

  • Code read-only and read-write properties for a class.
  • Use both instance and static properties.

Overview

Properties provide an elegant solution to a common design dilemma: public vs. private data. Public data provides clean access syntax but can break encapsulation. Private data and public access methods provide maximum encapsulation but can require awkward syntax in use. Properties give the best of both solutions.


part 1- Set-up

This part sets up the example we will use to experiment with properties.

steps:

  1. Create a class to represent a savings account. Add private fields for the account number and the account balance. Code a constructor to initialize the account number and the balance.

    class SavingsAccount
    {
    	private int    accountNumber;
    	private double balance;
    	
    	public SavingsAccount(int accountNumber, double balance)
    	{
    		...
    	}
    	...
    }
    
  2. Create a class called SavingsAccountTest and add a Main method. Create a Account and test your work.

    class SavingsAccountTest
    {
    	static void Main()
    	{
    		SavingsAccount account = new SavingsAccount(12345, 2000);
    		...
    	}
    	...
    }
    

part 2- Read-only instance properties

Here we add read-only instance properties.

steps:

  1. Add read-only instance properties AccountNumber and Balance to the savings account class. The implementation should simply return the current value of the accountNumber and balance fields. Test your work.

    class SavingsAccount
    {
    	public int    AccountNumber { ... }
    	public double Balance       { ... }
    	...
    }
    

part 3- Calculated property

Here we add a property whose value is calculated as needed, i.e. it is not backed by a field.

steps:

  1. Add the read-only instance property IsOverdrawn to the savings account class. The property should be true if the balance of the account is negative and false otherwise. Test your work.

    class SavingsAccount
    {
    	public bool IsOverdrawn { ... }
    	...
    }
    

part 4- Read-Write instance property

Here we add a property that supports both read and write access.

steps:

  1. Add the read-write instance property InterestEarned to the savings account class. The property should be backed by a field that stores the amount of interest the account has earned. Include validation code in the set accessor that ensures the value being assigned is positive. If an attempt is made to assign invalid data, print an error message and return without performing the assignment. Test your work.

    class SavingsAccount
    {
    	private double interestEarned; // field
    
    	public double InterestEarned { ... } // property
    
    	...
    }
    

part 5- Read-Write static property

Here we add a static property that supports both read and write access.

steps:

  1. Add the read-write static property InterestRate to the savings account class. The property is static because it applies to all accounts, i.e. all accounts earn the same rate of interest. The property should be backed by a static field that stores the value. Include validation code in the set accessor that ensures the value being assigned is positive. If an attempt is made to assign invalid data, print an error message and return without performing the assignment. Test your work.

    class SavingsAccount
    {
    	private static double interestRate; // field
    
    	public static double InterestRate { ... } // property
    
    	...
    }
    

Solutions:

This material is excerpted from the Programming C# course offered by DevelopMentor.