This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!
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}
where:
- 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.
- type
- The property type, which must be at least as accessible as the property itself. For more information on accessibility levels, see Access Modifiers.
- identifier
- The property name. For more information on interface member implementation, see interface.
- accessor-declaration
- Declaration of the property accessors, which are used to read and write the property.
- interface-type
- The interface in a fully-qualified property name. See Interface Properties.
Remarks
- 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.
Example
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.
// 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 {
get {
return name;
}
set {
name = value;
}
}
// A read-only static property:
public static int Counter {
get {
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);
}
}
Output
Employee number: 101
Employee name: Claude Vige
See Also
Accessors | Indexers | Properties