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) 2015, 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.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

Class Features

 "const" and "readonly" Variables

 Method Overloading

Properties

 Operators

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version