You are currently offline, waiting for your internet to reconnect

Your browser is out-of-date

You need to update your browser to use the site.

Update to the latest version of Internet Explorer

How to define and use properties in Visual C#

This article was previously published under Q319265
SUMMARY
This step-by-step article shows you how to define and use properties in a Visual C# application.

One important aim of many object-oriented programming languages is to allow you to encapsulate internal details in a class. Visual C# allows you to define properties to provide controlled access to internal details in a class.

A property can be read-only, write-only, or read-write. A get accessor provides read access, and a set accessor provides write access. You can add code to these accessors to provide the appropriate functionality to get or set the property.

In this article, you create a class named Employee. Within this class, you define a read-write property for the name of the employee, a property to read the salary of the employee, and a property to calculate how long the employee has worked for the company. You then create an Employee object in your application and use its properties.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
This article assumes that you are familiar with the following topics:
  • Visual C# syntax
  • Object-oriented concepts
back to the top

Create an Application That Contains a Visual C# .NET Class

  1. Start Visual Studio .NET or Visual Studio 2005, and create a new Visual C# Console Application project named EmployeeProperties. Class1.cs is created by default in Visual Studio .NET. Program.cs is created in Visual Studio 2005.
  2. Rename Class1.cs or Program.cs as Employeeapp.cs.
  3. On the Project menu, click Add Class. In the Add New Item dialog box, type Employee.cs in the Name text box, and then click Open.
back to the top

Define Fields, Constructors, and Methods

  1. Add three Private fields to the Employee class, as follows:
    private string Name;private decimal Salary;private DateTime Hiredate;					
  2. Define a constructor for the class to initialize the fields in the Employee class, as follows:
    public Employee(string Name, decimal Salary, DateTime Hiredate){    this.Name = Name;    this.Salary = Salary;    this.Hiredate = Hiredate;}					
  3. Add a Public method to the class, as follows:
    public void PayRise(decimal Amount){    this.Salary += Amount;}					
back to the top

Define Properties in the Class

  1. Add the following code to add a read-write property named FullName. The get accessor returns the name of the employee. The set accessor uses an implicit parameter called value that is passed into the set accessor to set the name of the employee.
    public string FullName{    get    {	return this.Name;    }    set    {	this.Name = value;    }}					
  2. Add the following code to add a read-only property named Remuneration. The get accessor returns the salary of the employee. There is no set accessor because the salary cannot be set directly; the only way to change the salary is to call the PayRise method.
    public decimal Remuneration{    get    {	return this.Salary;    }}					
  3. Add the following code to add another read-only property named DaysSinceJoined. The get accessor calculates how many days the employee has worked for the company.
    public int DaysSinceJoined{    get    {	TimeSpan Timespan = DateTime.Now.Subtract(Hiredate);	return Timespan.Days;    }}					
back to the top

Create an Object and Use the Properties

  1. Display the code for Employeeapp.cs in the Code View window.
  2. In the Main method, use the new operator to create an Employee object. Assign the object reference to a local Employee variable, as follows:
    Employee worker = new Employee("Jayne Doe",125000, new DateTime(1998,7,31));					
  3. Add the following code to change the FullName property of the employee. This implicitly invokes the set accessor for the FullName property:
    worker.FullName = "Jayne Lafayette";					
  4. Add the following code to display the employee details by using the get accessors for the FullName, Remuneration, and DaysSinceJoined properties:
    Console.WriteLine("Full name: {0}",worker.FullName);Console.WriteLine("Remuneration: {0}",worker.Remuneration);Console.WriteLine("Days since joined: {0}",worker.DaysSinceJoined);					
back to the top

Test the Application

  1. Build and run the application.
  2. The application displays the following information in the console (the "Days since joined" value depends on the current date):
    Full name: Jayne LafayetteRemuneration: 125000Days since joined: 1139						
  3. Return to the Employeeapp.cs file in the Code View window, and add the following code at the end of the Main method:
    worker.Remuneration = 100000;					
  4. Try to build the application. You receive a compilation error because Remuneration is a read-only property.
  5. Change the statement as follows:
    worker.Salary = 100000;					
  6. Try to build the application again. You receive another compilation error because the Salary field is marked as Private. The only way to modify the salary of the employee is to use the public PayRise method.
back to the top
Properties

Article ID: 319265 - Last Review: 12/11/2006 21:04:21 - Revision: 2.3

  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • kbhowtomaster KB319265
Feedback