It is possible to split the definition of a class or struct or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled( means a single dll is formed for all the partial class files with same name when compiled).
There are several situations when splitting a class definition is desirable:
- When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.
To split a class definition, use the Partial keyword modifier, as shown below:
//partial Class public partial class Employee { public void DoWork() { } } public partial class Employee { public void GoToLunch() { } } //partial Interface partial interface ITest { void Interface_Test(); } partial interface ITest { void Interface_Test2(); } //partial Struct partial struct S1 { void Struct_Test() { } } partial struct S1 { void Struct_Test2() { } }
When a Class definition is splitting over multiple source files using Partial keyword, one should keep in mind the following points.
- All the source files of the class must be within the one namespace/assembly. Partial definitions cannot span multiple modules.
- Classes in all source files must be preceeded with the keyword Partial.
- Classes in all source files must have the same accessability modifier.
- If any of the parts(class) 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.
- 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. For example, the following declarations:
partial class Earth : Planet, IRotate { }
partial class Earth : IRevolve { } equalent to class Earth : Planet, IRotate, IRevolve { }
- Any class, struct, or interface members declared in a partial definition are available to all of the other parts.
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() { } } }
At compile time, attributes of partial-type definitions are merged. For example, the above code will be merged like this.
class Container { class Nested { void Test() { } void Test2() { } } } example: In the following example, the fields and the constructor of the class, CoOrds, are declared in one partial class definition, while the member, PrintCoOrds, is declared in another partial class definition. 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() { System.Console.WriteLine("CoOrds: {0},{1}", x, y); } } class TestCoOrds { static void Main() { CoOrds myCoOrds = new CoOrds(10, 15); myCoOrds.PrintCoOrds(); } } output: 10,15
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.