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

C#

ADD STATIC METHODS

Astatic method maintains its information regardless of how many class instances are created; you can use static methods for maintaining a value such as the

boiling temperature of water. Like classes, methods are either static or instance members of the class. A static method contains information that will remain constant so the class can use it repeatedly. This is useful when you want to make calculations in your class with a value that is always constant.

You must explicitly include the static option before typing in the method keyword in your code. If you do not, then C# will automatically consider the method to be non-static. This chapter discusses non-static methods in greater detail later on.

If you declare a static modifier with your method, then you cannot also include a virtual, abstract, or override modifier. If you try to, the MDE window will point out the error and your project will not compile. The static modifier remains with that class and only with that class — it does not rely on any methods in any other inheriting or base class. Because virtual, abstract, and override modifiers deal with inheriting classes, they do not apply to static modifiers.

You cannot access static members through object instances that occur when you run your project. That is what nonstatic methods are for. You can access static methods through both value and reference types.

ADD STATIC METHODS

Visual C# Projects

Console

Application

The Start page appears.

The New Project

 

¤ Click New Project.

window appears.

 

 

Click the Console

 

 

 

 

 

 

 

 

Application icon in the

 

 

Templates pane.

Type a name for the file.

ˇ Click OK.

PROGRAMMING METHODS AND EVENTS 6

If you need to return more than one variable from your static method, you can do so using the params keyword.

 

TYPE THIS:

 

 

RESULT:

 

 

 

 

using System;

 

 

10

 

 

public class Params

 

 

15

 

 

{

 

 

20

 

 

public static void Parameter(params int[] list)

 

 

 

 

 

{

 

 

 

 

 

for ( int x = 0 ; x < list.Length ; x++

 

 

 

 

 

)

 

 

 

 

 

Console.WriteLine(list[x]);

 

 

 

 

 

Console.WriteLine();

 

 

 

 

 

}

 

 

 

 

 

public static void Main()

 

 

 

 

 

{

 

 

 

 

 

Parameter(10, 15, 20);

 

 

 

 

 

}

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

Class1

void

int

Add Method

Add

Á Click the Class View tab.

 

· Right-click the class

The C# Method Wizard

 

 

Click the plus sign (

)

name.

appears.

 

 

 

Click Add Add Method.

 

Type the method name in

next to the Method name.

 

 

 

 

 

 

 

 

° Click the plus sign (

)

 

 

 

the Method name field.

 

 

 

 

 

 

 

next to the {} Method name.

StaticMethod

Note: The Method signature field at the bottom reflects the changes to the method code as you type information into the wizard fields.

CONTINUED

123

C#

ADD STATIC METHODS

C# uses simple names for accessing many different elements in a C# project, and methods are no different. However, if you have a static method then

how you program static methods and other static information in your method determines if you can use simple names or not.

Simple names for a variable can be just one letter, such as x.

When you declare variables and associate them with value types, the methods you include those declarations in determine whether your program can process those variables. For example, you can declare two variables of integers with the simple names a and b, with a declared as a non-static member and b declared as a static member.

If you place the two variables in a non-static method and evaluate them later in your class, you will have no trouble with your evaluation. However, if you put those two variables in a static method you will only be able to evaluate the static variable b because a static method cannot access a non-static variable.

If you decide to plunge ahead anyway and try to evaluate a non-static variable in a static method, you will find that the MDE window will protest that action and your program will not compile until you fix the problem.

ADD STATIC METHODS (CONTINUED)

Class1.cs

select a method

The static method code

the Method

appears in the parent

check box area.

window.

.

 

¢ Move the static

 

 

method code above the Main

 

method code.

Type the Record class code that establishes variables and static methods for adding to the number of records.

PROGRAMMING METHODS AND EVENTS 6

You can reference a static method in what Visual Studio .NET refers to as a member-access format. The member-access format contains the full version of the type and its associated identifier. The member-access format comes in the form of the type, a period, and then the identifier. For example, you can have the member access type int.number.

C# and the Visual Studio .NET suite do not force you to use the member-access format because many of the access types have aliases that C# refers to. If you reference your static method (or any other static member) in member-access form you must do so in the form E.M. The E must stand for the type that your method uses, not the object. The M stands for the type identifier. For example, if your method is of the type integer with the name NumberOne, then the member access form is int.NumberOne.

§ Type the Main method that lets the user input values and outputs the results.

Press the F5 key.

Save the program as the

Type information at the

filename.

 

prompts and the output

 

appears.

 

125

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