Property Declaration
Collapse the table of content
Expand the table of content
The document is archived and information here might be outdated

Property Declaration

Properties are an extension of fields and are accessed using the same syntax. Unlike fields, properties do not designate storage locations. Instead, properties have accessors that read, write, or compute their values.

Property declaration takes one of the following forms:

[attributes] [modifiers] type identifier {accessor-declaration}
[attributes] [modifiers] type interface-type.identifier {accessor-declaration}


attributes (Optional)
Additional declarative information. For more information on attributes and attribute classes, see C# Attributes.
modifiers (Optional)
The allowed modifiers are new, static, virtual, abstract, override, and a valid combination of the four access modifiers.
The property type, which must be at least as accessible as the property itself. For more information on accessibility levels, see Access Modifiers.
The property name. For more information on interface member implementation, see interface.
Declaration of the property accessors, which are used to read and write the property.
The interface in a fully qualified property name. See Interface Properties.


Unlike fields, properties are not classified as variables. Therefore, it is not possible to pass a property as a ref or out parameter.

A property declared using the static modifier is classified as a static property; otherwise, it is classified as an instance property. Like other static members, a static property is not associated with a specific instance and cannot be referenced through an instance. Instead, it is associated with the type and can only be referenced through the type name. For example, in the following statements:

Button okButton = new Button();
// Using an instance property:
string s = okButton.Caption;

the Caption property is associated with the instance okButton. If Caption is declared as a static property, the class name (Button) must be used instead:

// Using a static property:
string s = Button.Caption;

An instance of a class can be accessed using this in the accessors of an instance property, but it is an error to use this in the accessors of a static property.

It is an error to use a virtual, abstract, or override modifier on an accessor of a static property.


This example demonstrates instance, static, and read-only properties. It accepts the name of the employee from the keyboard, increments numberOfEmployees by 1, and displays the Employee name and number.

// property.cs
// Properties
using System;
public class Employee 
   public static int numberOfEmployees;
   private static int counter;
   private string name;

   // A read-write instance property:
   public string Name 
         return name; 
         name = value; 

   // A read-only static property:
   public static int Counter 
         return counter; 

   // Constructor:
   public Employee() 
      // Calculate the employee's number:
      counter = ++counter + numberOfEmployees;

public class MainClass
   public static void Main() 
      Employee.numberOfEmployees = 100;
      Employee e1 = new Employee();
      e1.Name = "Claude Vige";  
      Console.WriteLine("Employee number: {0}", Employee.Counter);
      Console.WriteLine("Employee name: {0}", e1.Name);


Employee number: 101
Employee name: Claude Vige

See Also

Properties | Accessors | Indexers | Properties Tutorial

© 2016 Microsoft