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

C#

ADD AN INTERFACE INDEX

ike interfaces, you can add an interface index in a class. LThe MDE window lets you create an interface index in

your class. After you finish with the wizard, the index code will appear in the MDE window so you can edit it to your satisfaction.

An indexer works very much like an array, but the difference is that an array stores values and an indexer stores implementation information contained in one or more interfaces in your class. The interface index helps your program categorize and obtain interface information more quickly. This means that indexers are your friends because they increase the performance of your program.

The C# Indexer Wizard is a window containing several fields in which you can enter interface indexer information. This includes drop-down lists of default information that you can choose from and adding index parameters (such as the index name) to your index.

Visual C# bases the interface indexer type on the value or reference type that appears in your class. This lets you tie into the type of value that you want the index to affect. For example, if you have a variable with a byte value that you want the interface index to add its information to, be sure the indexer type is byte.

ADD AN INTERFACE INDEX

Console

Applicatio

Click the Console

Application icon in the

Templates pane.

Type a name for the file.

Click OK.

Á Click the Class View tab in the Solution Explorer window.

The Class View window appears.

Class View - Indexer

Class1

Click the plus sign (+) to expand the tree until you reach the Class1 entry.

Note: You can also view the Class View window by pressing Ctrl+Shift+C on your keyboard.

WORKING WITH TYPES AND INTERFACES 5

The public indexer access option in the C# Indexer Wizard lets you select the access modifier as you do when you create an instance constructor. You have your choice from one of five access modifiers.

The public modifier is the default setting; this ensures that your entire project, as well as any other program that accesses your project, can access your class.

The protected modifier limits access to the members of the base class and any inheriting classes.

The internal modifier limits access to any element in your project.

The protected internal modifier limits access to elements in your project or to the members of the base class and any inheriting classes.

The private modifier limits access to members only within the class.

The default accessibility option is public and that is the choice you see when you create a new interface index in the wizard.

Class View - Indexer

Class1

 

Add

 

Add Indexer...

° Right-click Class1.

· Click Add.

 

Click Add Indexer.

public

The C# Indexer Wizard window appears.

Click to select the indexer access type from the drop-down list.

CONTINUED

113

C#

ADD AN INTERFACE INDEX

fter you enter the indexer access level and type Ainformation, you can enter parameters that determine

what type and name the indexer will have. The type of indexer must be the same as the value or reference type that the interface index accesses. For example, if the reference type in the class is an object, you should give the interface index the object parameter type.

From there you can enter the parameter name from a list of parameters that meet the object type requirements. For example, if your interface index is an object, the list of available parameter names will be those in the class that associate with the object type. You can add and remove as many parameters from the indexer list as you want.

The indexer modifiers let you determine if the indexer will be a regular indexer or will have special instructions. For example, the virtual indexer will check to see if there is an inheriting class that has its own index that will override the index you are currently adding. If the inheriting class has an index with an override modifier, your class will use the override index instead.

You can also add comments to your index if you want information about your index present in your code.

ADD AN INTERFACE INDEX (CONTINUED)

int

int

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

WORKING WITH TYPES AND INTERFACES 5

Properties and indexers have some similarities — the most obvious is that all of the rules defined for the properties get and set accessors also apply to the indexer get and set accessors.

Although properties and indexers are related, you should be aware of some significant differences:

Visual C# identifies a property by its name and an indexer by its signature.

You can access a property with a simple name. You must access an indexer through an element.

A property can have a static object that does not change. An indexer must contain instance information generated by the class.

The get accessor of a property has no additional parameters. The get accessor of an indexer has the same parameters as the indexer.

The set accessor of a property contains the implicit value parameter. The set accessor of an indexer has the value parameter and the additional indexer parameters.

int

int Param1

Type the indexer parameter name(s) in the Parameter name field.

£ Add the name(s) to the Parameter list field by clicking the Add button.

¢ Click to select the indexer modifier from the Indexer modifiers area.

Type a comment for your indexer.

§ Click the Finish button.

The indexer code skeleton appears in your class code so you can edit it.

115

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