Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

COMPARE PROPERTIES AND INDEXERS

n the surface it may seem like properties and Oindexers (indeed, properties and many other C#

features) have many similarities, and to some degree that is true. After all, it makes little sense to reinvent the wheel for every C# feature. However, properties and indexers do have some important differences.

Both properties and indexers use the get and set arguments. The get argument reads and returns a value of the property type. The get value can also compute a value

(such as converting Fahrenheit to Celsius) and return the computed value.

The set argument writes the property received from the get argument and appears after the get argument. The

set argument is similar to a method that returns void because it writes the value of the property type and does not return the value.

In the case of the get and set accessors, indexers contain the same formal parameters as the indexer does but properties do not contain any parameters at all. Otherwise, property accessors have some greater flexibility than indexers: A property is identified by its name instead of its signature, you can access a property through a simple name or access through a class, struct, or interface member, and the property is a static or instance member.

COMPARE PROPERTIES AND INDEXERS

Solution Explo...

ution Explo...

appears.

¤ Click Help.

Click Index.

ACCESSING PROPERTIES 9

Properties contain some limitations that you should know about so you can avoid confusing property abilities with other features such as indexers and classes.

Because C# does not classify properties as variables, you cannot pass a property with the ref or out parameters. The ref and out parameters cause a method to refer to the same variable that the method acquired.

C# automatically classifies the property as an instance property unless you specifically add the static modifier in the argument. Your program cannot reference a static property through an instance but only through the type name.

If you declare a property as a static property, then you cannot use the this keyword or the virtual, abstract, or override modifiers.

If you want more information about properties and how they work differently from indexers, the MDE window online help contains downloadable examples of using properties in their properties tutorial. After you search for and access the properties tutorial page, you can access the samples by clicking the “Properties Sample” link on the page.

Index

The Index window appears.

Type properties in the Look for field.

ˇ Click to select Visual C# in the Filtered by dropdown list.

Á Click comparison between properties and accessors under properties in the topics list.

The Comparison Between Properties and Indexersv help file appears in the parent window.

187

C#

PROGRAM PROPERTY ACCESSORS

The get and set keywords comprise the property accessors. The get keyword reads information into the property, and the set keyword writes property

information into your class, struct, or interface for further processing in your program.

Any property includes the token get and/or set accessors, but if you want to do anything with your property, you must add the accessor body. The get accessor effectively reads the value of a field — either a value that you enter into the program itself or that the user enters during runtime.

After the get accessor retrieves the value, it must return the value with the return or throw statement. All return

statements in the get accessor must specify an expression, such as a string name, that can be converted to the property type. Unless another portion of your class, struct, or interface references a property as part of an expression, the get accessor computes the value of the property.

The set accessor acts much like a method with the void return type. The body of a set accessor provides a new value for the property. A set accessor must include a return type, but unlike the get accessor, that return type must not include an expression.

PROGRAM PROPERTY ACCESSORS

Solution Explo...

page appears. The New Project window

appears.

New Project.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

ACCESSING PROPERTIES 9

You can override a property accessor in one property with an accessor from another property.

TYPE THIS:

using System; class Test;

{

abstract int Area {get;

set;

}

class Cube: Area

{

public int side; public Cube(int s) {side = x;

}

public override int Area {get

{

return side * side;

}

set

{side = Math.Sqrt(value);

}}}

RESULT:

This code overrides the abstract Area property with the code for returning the cube side value.

Á Click the Class View tab.

Click the plus sign () next to the Method name.

° Click the plus sign () next to the {} Method name.

· Right-click Class1.

Click Add.

Click Add Property.

The C# Property Wizard window appears.

CONTINUED

189

C#

PROGRAM PROPERTY ACCESSORS

# provides two different approaches for adding Cproperty accessors. First, you can enter the accessors

entirely in code. Second, you can have C# provide you with the skeleton property information by adding the property in the C# Property Wizard.

You can access the C# Property Wizard via the Class View window. The Property Wizard contains all the basic building blocks of a property including the get and set accessors.

When you finish entering the accessors and all other property information in the Property Wizard, the basic get and/or set accessor skeletons appear so that you can add arguments into the skeletons.

After you add the accessor information, you add property values into the accessor bodies. The get accessor must adhere to the same rules as value-returning methods. Because the return type of a method that is not void must specify an expression that has the same type as the method, the get accessor must specify an expression that has the same property type.

The set accessor must also adhere to rules — in this case, the rules for void methods. Both set accessors and void methods require that any return statement does not specify an expression. If the set accessor completes normally, the accessor writes the property value and returns that value to the property caller.

PROGRAM PROPERTY ACCESSORS (CONTINUED)

int

int

¡ Click to select property type from the drop-down list.

ACCESSING PROPERTIES 9

The get accessor must end with a return or throw statement just as a method does. If you do not include a return or throw statement, your program will not compile.

TYPE THIS:

using System; class Return;

private string name; public string Name

{

get

{

}

}

RESULT:

This code will return an error because there is no return or throw type within the get argument.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Type the property name in

 

 

£ Type a property comment

The property skeleton code

Save the program as the

 

 

 

the Property name field.

 

in the Comment field.

appears in the parent

filename.

 

Leave the accessor and

 

 

 

¢ Click Finish.

window.

 

 

 

 

 

 

 

property modifiers radio

 

 

 

 

 

 

 

 

 

 

buttons selected with get/set

 

 

 

 

 

 

 

 

 

 

and None, respectively.

 

 

 

 

 

 

 

 

 

 

191

Соседние файлы в папке c#