#Significance:
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.
Partial Classes:
There are several situations when splitting a class definition is desirable:
#When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.
#When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.
#To split a class definition,
Example:
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 same namespace.
#All the parts must use the partial keyword.
and then All 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 part is declared abstract, then the whole type is considered abstract. If any part is declared sealed, then the whole type is considered sealed. 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. Parts can specify different base interfaces, and the final type implements all the interfaces listed by all the partial declarations. 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.
Note:
The partial modifier is not available on delegate or enumeration declarations.
The following example shows that 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() { }
}
}
And also, At compile time, attributes of partial-type definitions are merged. For example, consider the following declarations:
[SerializableAttribute]
partial class Moon { }
[ObsoleteAttribute]
partial class Moon { }
Merged to,
[SerializableAttribute]
[ObsoleteAttribute]
class Moon { }
and become the final one class with all combined attributes.
and also
partial class Earth : Planet, IRotate { }
partial class Earth : IRevolve { }
merged to,
class Earth : Planet, IRotate, IRevolve { }
Example of Partial Class:
public partial class CoOrds
{
private int x;
private int y;
public CoOrds(int x, int y)
{
this.x = x;
this.y = y;
}
}
public partial class CoOrds
{
public void PrintCoOrds()
{
Console.WriteLine("CoOrds: {0},{1}", x, y);
}
}
class TestCoOrds
{
static void Main()
{
CoOrds myCoOrds = new CoOrds(10, 15);
myCoOrds.PrintCoOrds();
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
// Output: CoOrds: 10,15
In the above, the fields and the constructor of the class, CoOrds, are declared in one partial class definition, and the member,PrintCoOrds, is declared in another partial class definition.
Partial Methods :
#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.
#Any code in the partial class can freely use a partial method, even if the implementation is not supplied. No compile-time or run-time errors will result if the method is called but not implemented.
#Partial methods are especially useful as a way to customize generated code. They allow for a method name and signature to be reserved, so that generated code can call the method but the developer can decide whether to implement the method.
#Much like partial classes, partial methods enable code created by a code generator and code created by a human developer to work together without run-time costs.
#A partial method declaration consists of two parts:
-The definition, and the implementation.
These may be in separate parts of a partial class, or in the same part. If there is no implementation declaration, then the compiler optimizes away both the defining declaration and all calls to the method.
// Definition in file1.cs
partial void onNameChanged();
// Implementation in file2.cs
partial void onNameChanged()
{
// method body
}
Comments
Post a Comment