Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
18
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 6 MORE ABOUT CLASSES

Figure 6-20. Hierarchy of strictly restrictive accessor levels

Partial Classes and Partial Types

The declaration of a class can be partitioned among several partial class declarations.

Each of the partial class declarations contains the declarations of some of the class members.

The partial class declarations of a class can be in the same file or in different files.

Each partial declaration must be labeled as partial class, in contrast to the single keyword class. The declaration of a partial class looks the same as the declaration of a normal class, other than the addition of the type modifier partial.

Type modifier

partial class MyPartClass // Same class name as following

{

member1 declaration member2 declaration

...

}

Type modifier

partial class MyPartClass // Same class name as preceding

{

member3 declaration member4 declaration

...

}

Note The type modifier partial is not a keyword, so in other contexts you can use it as an identifier in your program. But when used immediately before the keywords class, struct, or interface, it signals the use of a partial type.

157

CHAPTER 6 MORE ABOUT CLASSES

For example, the box on the left of Figure 6-21 represents a file with a class declaration. The boxes on the right of the figure represent that same class declaration split into two files.

Figure 6-21. Class split using partial types

All the partial class declarations comprising a class must be compiled together. A class using partial class declarations has the same meaning as if all the class members were declared within a single class declaration body.

Visual Studio uses this feature in its standard Windows program templates. When you create an ASP.NET project or a Windows Forms project from the standard templates, the templates create two class files for each web page or form:

One file contains the partial class containing the code generated by Visual Studio, declaring the components on the page. You shouldn’t modify the partial class in this file, since it’s regenerated by the Visual Studio when you modify the components on the page.

The other file contains the partial class you use to implement the look and behavior of the components of the page or form.

Besides partial classes, you can also create two other partial types, which are the following:

Partial structs. (Structs are covered in Chapter 12.)

Partial interfaces. (Interfaces are covered in Chapter 17.)

158

CHAPTER 6 MORE ABOUT CLASSES

Partial Methods

Partial methods are methods that are declared in two parts of a partial class. The two parts of the partial method can be declared in different parts of the partial class or in the same part. The two parts of the partial method are the following:

The defining partial method declaration:

Gives the signature and return type.

The implementation part of the declaration consists of only a semicolon.

The implementing partial method declaration:

Gives the signature and return type.

The implementation is in the normal format, which, as you know, is a statement block.

The important things to know about partial methods are the following:

Both the defining and implementing declaration must match in signature and return type. The signature and return type have the following characteristics:

The contextual keyword partial must be included in both the defining and implementing declarations immediately before the keyword void.

The signature cannot include access modifiers, making partial methods implicitly private.

The return type must be void.

The parameter list cannot contain out parameters.

You can have a defining partial method without an implementing partial method. In this case, the compiler removes the declaration and any calls to the method made inside the class. If, however, the class has an implementing partial method, it must also have a defining partial method.

159

CHAPTER 6 MORE ABOUT CLASSES

The following code shows an example of a partial method called PrintSum.

PrintSum is declared in different parts of partial class Myclass: the defining declaration in the first part and the implementing declaration in the second part. The implementation prints out the sum of its two integer parameters.

Since partial methods are implicitly private, PrintSum cannot be called from outside the class. Method Add is a public method that calls PrintSum.

Main creates an object of class MyClass and calls public method Add, which calls method PrintSum, which prints out the sum of the input parameters.

partial class MyClass

 

 

{

Must be void

 

 

 

 

 

 

partial void PrintSum(int x, int y);

// Defining partial method

 

 

Contextual keyword

No implementation

 

public void Add(int x, int y)

 

 

{

 

 

 

PrintSum(x, y);

 

 

}

}

 

 

 

 

 

partial class MyClass

 

 

{

partial void PrintSum(int x, int y)

// Implementing partial method

 

 

{

 

← Implementation

 

Console.WriteLine("Sum is {0}", x + y);

}

}

class Program

{

static void Main( )

{

var mc = new MyClass(); mc.Add(5, 6);

}

}

This code produces the following output:

Sum is 11

160

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]