Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Partial methods

The throw statement | The try statement | The using statement | The yield statement | Using alias directives | Using namespace directives | Namespace alias qualifiers | Class base specification | Type parameter constraints | Partial types |


Читайте также:
  1. Alternative Methods
  2. Characterization methods and instruments
  3. Choice of methods
  4. Choose methods according to cost, targeting and response
  5. Choosing methods
  6. Classification of methods of radio therapy
  7. Conditional methods

Partial methods can be defined in one part of a type declaration and implemented in another. The implementation is optional; if no part implements the partial method, the partial method declaration and all calls to it are removed from the type declaration resulting from the combination of the parts.

Partial methods cannot define access modifiers, but are implicitly private. Their return type must be void, and their parameters cannot have the out modifier. The identifier partial is recognized as a special keyword in a method declaration only if it appears right before the void type; otherwise it can be used as a normal identifier. A partial method cannot explicitly implement interface methods.

There are two kinds of partial method declarations: If the body of the method declaration is a semicolon, the declaration is said to be a defining partial method declaration. If the body is given as a block, the declaration is said to be an implementing partial method declaration. Across the parts of a type declaration there can be only one defining partial method declaration with a given signature, and there can be only one implementing partial method declaration with a given signature. If an implementing partial method declaration is given, a corresponding defining partial method declaration must exist, and the declarations must match as specified in the following:

An implementing partial method declaration can appear in the same part as the corresponding defining partial method declaration.

Only a defining partial method participates in overload resolution. Thus, whether or not an implementing declaration is given, invocation expressions may resolve to invocations of the partial method. Because a partial method always returns void, such invocation expressions will always be expression statements. Furthermore, because a partial method is implicitly private, such statements will always occur within one of the parts of the type declaration within which the partial method is declared.

If no part of a partial type declaration contains an implementing declaration for a given partial method, any expression statement invoking it is simply removed from the combined type declaration. Thus the invocation expression, including any constituent expressions, has no effect at run-time. The partial method itself is also removed and will not be a member of the combined type declaration.

If an implementing declaration exist for a given partial method, the invocations of the partial methods are retained. The partial method gives rise to a method declaration similar to the implementing partial method declaration except for the following:

If a defining declaration but not an implementing declaration is given for a partial method M, the following restrictions apply:

Partial methods are useful for allowing one part of a type declaration to customize the behavior of another part, e.g., one that is generated by a tool. Consider the following partial class declaration:

partial class Customer
{
string name;

public string Name {

get { return name; }

set {
OnNameChanging(value);
name = value;
OnNameChanged();
}

}

partial void OnNameChanging(string newName);

partial void OnNameChanged();
}

If this class is compiled without any other parts, the defining partial method declarations and their invocations will be removed, and the resulting combined class declaration will be equivalent to the following:

class Customer
{
string name;

public string Name {

get { return name; }

set { name = value; }
}
}

Assume that another part is given, however, which provides implementing declarations of the partial methods:

partial class Customer
{
partial void OnNameChanging(string newName)
{
Console.WriteLine(“Changing “ + name + “ to “ + newName);
}

partial void OnNameChanged()
{
Console.WriteLine(“Changed to “ + name);
}
}

Then the resulting combined class declaration will be equivalent to the following:

class Customer
{
string name;

public string Name {

get { return name; }

set {
OnNameChanging(value);
name = value;
OnNameChanged();
}

}

void OnNameChanging(string newName)
{
Console.WriteLine(“Changing “ + name + “ to “ + newName);
}

void OnNameChanged()
{
Console.WriteLine(“Changed to “ + name);
}
}

Name binding

Although each part of an extensible type must be declared within the same namespace, the parts are typically written within different namespace declarations. Thus, different using directives (§9.4) may be present for each part. When interpreting simple names (§7.5.2) within one part, only the using directives of the namespace declaration(s) enclosing that part are considered. This may result in the same identifier having different meanings in different parts:

namespace N
{
using List = System.Collections.ArrayList;

partial class A
{
List x; // x has type System.Collections.ArrayList
}
}

namespace N
{
using List = Widgets.LinkedList;

partial class A
{
List y; // y has type Widgets.LinkedList
}
}


Дата добавления: 2015-11-16; просмотров: 52 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Type parameters and constraints| Class members

mybiblioteka.su - 2015-2024 год. (0.006 сек.)