Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp - Your Visual Blueprint For Building .NET Applications (2002) [eng].pdf
Скачиваний:
37
Добавлен:
16.08.2013
Размер:
9.71 Mб
Скачать

C#

VIEW INFORMATION ABOUT METHODS

Amethod is a piece of code that implements an action that a class or object can perform. Methods appear within a class and provide additional information that

classes cannot handle.

C# supports two types of methods: static and non-static. All objects in a class can access the static methods in that class without creating any instance of that class. Instances of a class can only access non-static methods. For more information on adding static and non-static methods, see pages 6 to 13.

You can overload methods, which means that different methods can have the same name as long as each separate

method has a unique signature. C# identifies a signature by looking for specific method features including the method name and the method parameters.

You can only add a method when you are editing a class. When you program a method you can do so in one of two ways: in code or by using the C# Method Wizard. The C# Method Wizard contains fields with basic method information that you can enter and choose from. Once you finish entering information into the wizard, the basic method code information appears in your code so you can edit it.

VIEW INFORMATION ABOUT METHODS

Index

Crtl+Alt+F2

Click Start Programs

The Start page appears.

 

¤ Click Help.

 

 

Click Index.

 

 

Microsoft Visual Studio .NET

 

 

 

 

 

 

7.0 Microsoft Visual Studio

 

 

 

 

 

 

.NET 7.0.

 

 

 

 

 

 

PROGRAMMING METHODS AND EVENTS 6

When you add a new method, you can have several methods with the same name with different signatures in the same class. However, if you try to add a method and another class type such as an interface with the same name, the MDE window would register an error and the compiler would not be able to run the program. If you have the same name for the method and interface but the method and interface were in separate classes, then C# would have no problem.

Though C# looks for the module name and the formal parameter list when determining a module signature, it does not look for the return type or the names of the parameters. So if you receive an error from the MDE window about signatures, check to see that your module names and lists are different for each module.

methods, adding in

adding in C#

adding in C#

The Index window appears.

Type methods in the Look for field.

ˇ Click the adding in C# entry in the Index list box.

The C# Add Method Wizard appears so you can learn about adding methods.

117

C#

ADD A METHOD

As with a property and an indexer, C# gives you two ways to add a method. If you like the step-by-step functionality provided by a wizard, the C# Add

Method Wizard lets you add a method automatically. You can also add a method in code.

When you add a method in code you start with the method keyword. You can add information that precedes the keyword: whether the method is static or non-static (the default is non-static) and whether the method contains a void type. The void type renders the method invisible where it takes on no parameters and returns no values.

After you enter the method keyword, you can enter the optional method declarations. These declarations include various attributes, method modifiers, the return type, and then the name of the method itself. Then you begin to add the information within your method.

Attributes include names that you can enter in your class and refer to in your method. An attribute is a good way to identify information that you want to include in your class such as your company Web site. The method modifiers help determine the access to your method from your class and other code in your project.

ADD A METHOD

Visual C# Projects

Console

Application

 

The Start page appears.

The New Project window

.NET

 

¤ Click New Project.

appears.

Studio

 

 

Click the Console

 

 

 

 

 

 

 

 

 

 

Application icon in the

 

 

 

Templates pane.

Type a name for the file.

ˇ Click OK.

PROGRAMMING METHODS AND EVENTS 6

You use the return keyword in all methods except one: the void type. When you specify the void method type, you do not need to include the return keyword because the return type is automatically void.

 

TYPE THIS:

 

 

RESULT:

 

 

 

using System;

 

 

The diameter is 25

 

class VoidTest

 

 

 

 

{

 

 

 

 

public static void Main()

 

 

 

 

{

 

 

 

 

int diameter = 25;

 

 

 

 

Console.WriteLine("The diameter is {0}", diameter);

 

 

 

 

}

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

Class View - M. . .

Method

{}Method

Class1

Public

Void

int

Add Method . . .

Add

Á Click the Class View tab.

 

· Right-click the class name

The C# Method Wizard

 

 

Click the plus sign (

)

 

to open the pop-up menu.

window appears.

 

 

 

Click Add Add Method.

 

 

next to the Method name.

 

 

 

 

 

 

 

 

° Click the plus sign (

)

 

 

 

 

 

 

 

 

 

 

next to the {} Method name.

 

 

 

 

CONTINUED

119

C#

ADD A METHOD

After you include the attributes and method access modifiers, you can further define your method using several different modifiers.

If your method resides in an inheriting class and you also have a modifier in your base class, you can disregard the method in the base class by adding the new keyword. Using the new keyword in your method effectively hides the base class method so your class only pays attention to the method in its class.

You can determine if the method will have the static, virtual, override, abstract, or extern status. A static method lets all objects in its class access it. You can use a virtual

method in an inheriting class; a virtual method checks to see if any methods in any related class must override that method. An override method tells that method to override any methods in any related classes. The abstract method introduces a new virtual method but acts as a placeholder for a different method in a related class that will override the abstract method. The extern modifier lets you create an external method.

Once you add the modifier you can determine the return type and then enter the name of the method. After you add the method name you can begin work on the body of your method.

ADD A METHOD (CONTINUED)

void

int

Int Parameter1

The Method signature field at bottom reflects the changes to method code as you type

information into the wizard fields.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

± Type the parameter name

 

The added parameter

 

 

in the Parameter name field.

name appears in the

 

 

¡ Click Add.

Parameter list field.

 

 

 

 

 

PROGRAMMING METHODS AND EVENTS 6

C# lets you return multiple values from one method by using the out parameter.

 

TYPE THIS:

 

 

RESULT:

 

 

 

 

 

 

 

 

 

 

using System;

 

 

0

 

 

public class OutTest

 

 

25

 

 

{

 

 

 

 

 

public static int Output(out int a)

 

 

 

 

 

{

 

 

 

 

 

a = 25;

 

 

 

 

 

return 0;

 

 

 

 

 

}

 

 

 

 

 

public static void Main()

 

 

 

 

 

{

 

 

 

 

 

int a;

 

 

 

 

 

Console.WriteLine(Output(out a));

 

 

 

 

 

Console.WriteLine(a);

 

 

 

 

 

}

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

void

int

int Parameter1

Click to select a method modifier from the Method modifiers check box area.

£ Type a comment in the Comment field.

¢ Click Finish.

The method code appears in the parent window.

121

Соседние файлы в предмете Программирование на C++