How to define and use properties in Visual C#


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.


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

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.

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;

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
    return this.Name;
    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
    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
    TimeSpan Timespan = DateTime.Now.Subtract(Hiredate);
    return Timespan.Days;

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);

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 Lafayette
    Remuneration: 125000
    Days 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.

ID articol: 319265 - Ultima examinare: 11 iul. 2008 - Revizie: 1