What is wrapper methods in c net

Partial Classes and Methods (C # Programming Guide)

It is possible to split the definition of a class, a structure, an interface or a method into two or more source files. Each source file contains a section of the type or method definition. The parts are combined when the application is compiled.

Sub-classes

There are several situations in which it is desirable to split a class definition:

  • When you're working on large projects, splitting a class into multiple files allows multiple programmers to work on it at the same time.

  • When working with auto-generated sources, code can be added to a class without rebuilding the source file. Visual Studio takes this approach when building Windows Forms, web service wrapper code, and so on. You can write code that uses these classes without changing the file that Visual Studio created.

  • To split a class definition, use the partial keyword modifier, as shown here:

The keyword indicates that other parts of the class, structure, or interface can be defined in the namespace. All parts must use the keyword. All parts must be available at compile time to form the final type. All parts must have the same access level, e.g., etc.

If any part is declared abstract, the whole type is then considered abstract. If any part is declared sealed, then the whole type is considered sealed. If any part declares a base type, the whole type inherits that class.

All parts that specify a base class must match, but parts that omit a base class still inherit the base type. Parts can specify different base interfaces, and the last type implements all of the interfaces listed by all part declarations. All class, structure and interface members declared in a part definition are available to all other parts. The final type consists of all parts at compile time.

Note

The modifier is not available for delegate or enumeration declarations.

The following example shows that nested types can be partially executed even if the type in which they are nested is not itself.

Attributes from partial type definitions are merged at compile time. For example, consider the following declarations:

They correspond to the following declarations:

The following are merged from all partial type definitions:

  • XML comments

  • Interfaces

  • Generic parameter attributes

  • Class attributes

  • Member

For example, consider the following declarations:

They correspond to the following declarations:

Restrictions

There are several rules to keep in mind when using partial class definitions:

  • All partial type definitions that are intended as parts of the same type must also be changed. For example, the following class declarations produce an error:

  • The modifier can only appear immediately before the keywords, or.

  • Nested partial types are allowed in partial type definitions, as shown in the following example:

  • All partial type definitions that are intended to be parts of the same type must be defined in the same assembly and module (EXE or DLL file). Partial definitions cannot span multiple modules.

  • The class name and generic type parameters must match all partial type definitions. Generic types can be partial. Each partial definition must have the same parameter names in the same order.

  • The following keywords for a partial type definition are optional, but if they exist for a partial definition, they cannot conflict with other keywords specified in other partial definitions of the same type:

For more information, see Limitations on Type Parameters.

example 1

description

In the following example, the fields and constructor of the class () are declared in one partial class definition and the member () is declared in another partial class definition.

code

Example 2

description

The following example shows that you can also develop partial structures and interfaces.

code

Partial methods

A partial class or struct can contain a partial method. Part of the class contains the signature of the method. An implementation can be defined in the same or in a different part. If the implementation is not provided, then the method and all calls to the method are removed at compile time. Implementation may be required depending on the method signature.

Partial methods allow the implementer of one part of a class to define a method that is similar to an event. The implementor of the other part of the class can decide whether to implement the method. If the method is not implemented, the compiler can then remove the method signature and all calls to the method. The calls to the method, including results that would occur if an argument were evaluated in the call, have no effect at runtime. Therefore, any code in the partial class is free to use a partial method even if the implementation is not provided. There are no run-time or compile-time errors if the method is called but not implemented.

Partial methods are particularly useful for customizing generated code. They allow the method name and signature to be reserved so that generated code can call the method but the developer can decide how to implement the method. Like partial classes, partial methods allow code created by a code generator to run along with code created by a human developer at no runtime cost.

A partial method declaration consists of two parts: the definition and the implementation. These can be in separate parts of a partial class or in the same part. If there is no implementation declaration, the compiler then optimizes both the defining declaration and all calls to the method.

  • Partial method declarations must begin with the context keyword partial.

  • Partial method signatures in both parts of the partial type must match.

  • Partial methods can have static and unsafe modifiers.

  • Partial methods can be generic. Restrictions apply to the defining partial method declaration and can optionally be repeated for the implementing one. Parameter and type parameter names do not have to be the same in the implementing and defining declaration.

  • You can create a delegate for a partial method that has been defined and implemented. However, this does not work for partial methods that have only been defined.

A partial method does not require implementation in the following cases:

  • It has no access modifiers (including the default private).

  • The return value is void.

  • It has no out parameters.

  • It does not have any of the following modifiers: virtual, override, sealed, new, or extern.

Any method that does not meet all of the constraints (such as the method) must provide an implementation.

C # programming language specification

For more information, see Partial Data Types in the C # Language Specification. The language specification is the authoritative source for the syntax and usage of C #.

See also