Microsoft KB Archive/319265

= How to define and use properties in Visual C# =

Article ID: 319265

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q319265



For a Microsoft Visual Basic .NET version of this article, see 308230.

IN THIS TASK
SUMMARY
 * Requirements
 * Create an Application That Contains a Visual C# .NET Class
 * Define Fields, Constructors, and Methods
 * Define Properties in the Class
 * Create an Object and Use the Properties
 * Test the Application



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
  Add three Private fields to the Employee class, as follows: private string Name; private decimal Salary; private DateTime Hiredate;   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; }                     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
  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; } }                     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; } }                     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; } }                   </li></ol>

back to the top

Create an Object and Use the Properties
 Display the code for Employeeapp.cs in the Code View window.</li>  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(&quot;Jayne Doe&quot;,125000, new DateTime(1998,7,31)); </li>  Add the following code to change the FullName property of the employee. This implicitly invokes the set accessor for the FullName property: worker.FullName = &quot;Jayne Lafayette&quot;; </li>  Add the following code to display the employee details by using the get accessors for the FullName, Remuneration, and DaysSinceJoined properties: Console.WriteLine(&quot;Full name: {0}&quot;,worker.FullName); Console.WriteLine(&quot;Remuneration: {0}&quot;,worker.Remuneration); Console.WriteLine(&quot;Days since joined: {0}&quot;,worker.DaysSinceJoined); </li></ol>

back to the top

Test the Application
 Build and run the application.</li> The application displays the following information in the console (the &quot;Days since joined&quot; value depends on the current date):

<pre class="fixed_text">Full name: Jayne Lafayette Remuneration: 125000 Days since joined: 1139

</li>  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; </li> Try to build the application. You receive a compilation error because Remuneration is a read-only property.</li>  Change the statement as follows: worker.Salary = 100000; </li> 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.</li></ol>

back to the top

Keywords: kbhowtomaster KB319265

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.