Using the partial keyword indicates that other parts of the class, struct, or interface can be defined within the namespace. All the parts must use the partial keyword. All of the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.
If any of the parts are declared abstract, then the entire type is considered abstract. If any of the parts are declared sealed, then the entire type is considered sealed. If any of the parts declare a base type, then the entire type inherits that class.
All of the parts that specify a base class must agree, but parts that omit a base class still inherit the base type. Parts can specify different base interfaces, and the final type implements all of the interfaces listed by all of the partial declarations. Any class, struct, or interface members declared in a partial definition are available to all of the other parts. The final type is the combination of all the parts at compile time.
Note |
|---|
| The partial modifier is not available on delegate or enumeration declarations. |
Nested types can be partial, even if the type they are nested within is not partial itself. For example:
class Container
{
partial class Nested
{
void Test() { }
}
partial class Nested
{
void Test2() { }
}
}
[System.SerializableAttribute]
partial class Moon { }
[System.ObsoleteAttribute]
partial class Moon { }
are equivalent to:
[System.SerializableAttribute]
[System.ObsoleteAttribute]
class Moon { }
For example, the following declarations:
partial class Earth : Planet, IRotate { }
partial class Earth : IRevolve { }
are equivalent to:
class Earth : Planet, IRotate, IRevolve { }