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

C#

INSERT THE VOID TYPE

The void type is a new type introduced with Visual C# and the last of the four types available. Visual C# uses the void type with methods as well as functions that

require methods including classes, events, delegates, and indexers.

The void type indicates that the method does not return a value and/or take any parameters. Many statements will use the void type that precedes the method so the program will understand that it will take the implementation information from your method and the method will not accept any parameters or return any value.

If you want a method to accept parameters from the code that accesses the method (such as a class) but not return any value, you can enter void as the return type. The void type cannot be used as a parameter in the method statement; void applies only to the method return type and as a precedent to the method statement.

The void type gets a lot of exposure because so many different components in Visual C# use methods. These can include indexers and events as well as other reference types including classes and delegates. See page 130 to learn more about using the void type with delegates.

INSERT THE VOID TYPE

Console

Applicatio

Properties

¤ Click New Project.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The New Project window

 

 

 

 

 

 

 

 

 

 

Type a name for the file.

 

appears.

 

ˇ Click OK.

 

 

Click the Console

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Application icon in the

 

 

 

 

 

 

Templates pane.

 

 

 

 

 

WORKING WITH TYPES AND INTERFACES 5

When you create a new class, you can use the new modifier for hiding an inherited member in the base class. You can do this by entering the name of the method preceded by the void type.

TYPE THIS:

using System;

public class Inherited : Base

{

new public void Main ();

}

RESULT:

This code hides the Main method in the base class so only the objects in the inherited class will receive the implementation instructions from the Main method. Because the void type precedes the Main method in the code, the method will not return any values or accept any variables.

You use the void type when the method has no return statement, but if you do not include a void type or a return statement within your method, the Visual Studio.NET compiler will return an error. The MDE window alerts you if a void type or return statement does not exist, so that you can fix the problem before you compile your program.

When you create a new class, you can use the new modifier for hiding an inherited member in the

The appears window

Á Delete within

C#

ADD INTERFACE PROPERTIES

he Visual C# Add Properties Wizard lets you enter Tproperties information from the Class View window

without entering any code. After you finish with the wizard, the properties information appears in your code in the proper location.

Properties provide basic information about how to read, write, and compute values of fields. Interface properties use the get and set accessors, statements that access information, for reading and writing information from a field, respectively.

When a user enters information into a text field in your program, you can use the get accessor to add that

information into your program and you can use the set accessor for assigning that user input to a value. The get accessor is similar to a method in that it must return a value of the property type. For example, if the property for the get accessor is character based, the value must be a string.

The set accessor is similar to a method that returns the void type. The set accessor is not designed to write information for output but to provide information acquired through the get accessor for use in the rest of the program. For example, a name acquired through the get accessor can be assigned to a value by using the set accessor.

ADD INTERFACE PROPERTIES

Console

Applicatio

 

Click the Console

Á Click the Class View tab

 

Application icon in the

in the Solution Explorer

Templates pane.

window.

 

Type a name for the file.

The Class View window

 

 

Click OK.

appears.

 

Note: You can also view the Class

 

 

 

 

View window by pressing Ctrl+Shift+C

 

 

on your keyboard.

Class View - IntProp

Add

Add Property...

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

° Right-click Class1.

· Click Add.

Click Add Property.

WORKING WITH TYPES AND INTERFACES 5

You can change the state of your object as the program runs within the get accessor, such as adding two plus (+) operators to an integer variable to change the variable value.

TYPE THIS:

using System; class ChangeState

{

public int Number get

{

return Number++;

}

}

RESULT:

The state of the object changes every time your project accesses the Number field.

Visual C# classifies the get and set accessors as read-only and write-only properties, respectively. Read-only properties cannot have any values written to them. Writeonly properties have restricted reference access — only properties that can use the write-only property to perform a task can reference that write-only property.

The C# appears.

Type in the access, type,

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