Thursday, June 25, 2009

Partial in c#

Introduction:

In a nutshell, partial classes mean that your class definition can be split into multiple physical files.

So it is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.

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

public partial class Employee
{
public void DoWork()
{
}
}

public partial class Employee
{
public void GoToLunch()
{
}
}

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace.

Features:

1. All the parts must use the partial keyword.
2. All the parts must be available at compile time to form the final type.
3. All the parts must have the same accessibility, such as public, private, and so on.
4. If any part is declared abstract, then the whole type is considered abstract.
5. If any part is declared sealed, then the whole type is considered sealed.
6. If any part declares a base type, then the whole type inherits that class. All the parts that specify a base class must agree, but parts that omit a base class still inherit the base type.
7. Parts can specify different base interfaces, and the final type implements all the interfaces listed by all the partial declarations.
8. Any class, struct, or interface members declared in a partial definition are available to all the other parts. The final type is the combination of all the parts at compile time.
9. The partial modifier is not available on delegate or enumeration declarations.

Nested Types with partial:

Nested types can be partial, even if the type they are nested within is not partial itself.

class Container
{
partial class Nested
{
void Test() { }
}
partial class Nested
{
void Test2() { }
}
}


Attributes with partial:

At compile time, attributes of partial-type definitions are merged. For example, consider the following declarations:

[SerializableAttribute]
partial class Moon { }

[ObsoleteAttribute]
partial class Moon { }

They are equivalent to the following declarations:

[SerializableAttribute]
[ObsoleteAttribute]
class Moon { }


Restrictions:

1. All partial-type definitions meant to be parts of the same type must be modified with partial.
2. All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.
3. The class name and generic-type parameters must match on all partial-type definitions. Generic types can be partial. Each partial declaration must use the same parameter names in the same order.
4. The following keywords on a partial-type definition are optional, but if present on one partial-type definition, cannot conflict with the keywords specified on another partial definition for the same type: public, private, protected, internal, abstract, sealed, base class, new modifier (nested parts), generic constraints.

Advantages

They allow programmers on your team to work on different parts of a class without needing to share the same physical file. While this is useful for projects that involve big class files, be wary: If you find your class file getting too large, it may well signal a design fault and refactoring may be required.

The most compelling reason for using partial class is to separate your application business logic from the designer-generated code. For example, the code generated by Visual Studio 2005 for a Windows Form is kept separate from your business logic (we will discuss this in a later section). This will prevent developers from messing with the code that is used for the UI. At the same time, it will prevent you from losing your changes to the designer-generated code when you change the UI.

Partial Methods

Partial methods are methods living in partial classes which are marked as partial.

Features:

1. A partial class or struct may contain a partial method. One part of the class contains the signature of the method. An optional implementation may be defined in the same part or another part. If the implementation is not supplied, then the method and all calls to the method are removed at compile time.
2. Partial method declarations must begin with the contextual keyword partial and the method must return void.
3. Partial methods can have ref but not out parameters.
4. Partial methods are implicitly private, and therefore they cannot be virtual.
5. Partial methods cannot be extern, because the presence of the body determines whether they are defining or implementing.
6. Partial methods can have static and unsafe modifiers.
7. Partial methods can be generic.
8. Constraints are put on the defining partial method declaration, and may optionally be repeated on the implementing one. Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining one.
9. You can make a delegate to a partial method that has been defined and implemented, but not to a partial method that has only been defined.

0 comments: