C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
Properties
This section describes what is a property - a named value of an object which is supported by a 'set' method and a 'get' method defined in the class.
Properties are like fields. Values can be assigned to and retrieved from properties in the same way as fields. But the declaration of properties requires a get method and a set method. When a value is assigned to a property, its set method will be executed. Similarly, when the value is retrieved from a property, its get method will be executed. There is no memory directly allocated to a property. If the value is truly needed to be stored, use another field to help.
The following program illustrates how a property can be used:
// Properties.cs // Copyright (c) 2008 HerongYang.com. All Rights Reserved. using System; class Properties { private string a; public string author { // property declaration get { return a; } set { a = value; // value is an implicit parameter } } public static void Main() { Properties p = new Properties(); p.author = "Herong Yang"; Console.WriteLine("p.author = {0}.", p.author); } }
Output:
p.author = Herong Yang.
Table of Contents
Logical Expressions and Conditional Statements
Visual C# 2010 Express Edition
"const" and "readonly" Variables
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation