13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8 <strong>Language</strong> Overview}static void F(int a, int b) {Console.WriteLine("F(int, int)");}static void F(int[] values) {Console.WriteLine("F(int[])");}static void Main() {F();F(1);int i = 10;F(ref i);F((object)1);F(1, 2);F(new int[] {1, 2, 3});}shows a class with a number of methods called F. The output produced isF()F(int)F(ref int)F(object)F(int, int)F(int[])8.7.4 PropertiesA property is a member that provides access to a characteristic of an object or a class. Examples ofproperties include the length of a string, the size of a font, the caption of a window, the name of a customer,and so on. Properties are a natural extension of fields. Both are named members with associated types, andthe syntax for accessing fields and properties is the same. However, unlike fields, properties do not denotestorage locations. Instead, properties have accessors that specify the statements to be executed when theirvalues are read or written.Properties are defined with property declarations. The first part of a property declaration looks quite similarto a field declaration. The second part includes a get accessor and/or a set accessor. In the example below,the Button class defines a Caption property.public class Button{private string caption;}public string Caption {get {return caption;}}…set {caption = value;Repaint();}Properties that can be both read and written, such as Caption, include both get and set accessors. The getaccessor is called when the property’s value is read; the set accessor is called when the property’s value iswritten. In a set accessor, the new value for the property is made available via an implicit parameter namedvalue.The declaration of properties is relatively straightforward, but the real value of properties is seen when theyare used. For example, the Caption property can be read and written in the same way that fields can be readand written:35

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!