Partial Classes

It is possible to split the definition of a class or a struct, an interface over two or more source files. Each source file contains a section of the type definition, and all parts are combined when the application is compiled. There are several situations when splitting a class definition is desirable like

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.

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. 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, internal, 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. 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.

Add 2 new code files under the project Part1.cs and Part2.cs and write the following code
using System;
namespace OOPSProject {
	partial class Parts {
		public void Method1() {
			Console.WriteLine("Method 1");
		}
		public void Method2() {
			Console.WriteLine("Method 2");
		}
	}
}
using System;
namespace OOPSProject {
	partial class Parts {
		public void Method3() {
			Console.WriteLine("Method 3");
		}
		public void Method4() {
			Console.WriteLine("Method 4");
		}
	}
}
Add a new class TestParts.cs under the project and write the following code:
class TestParts {
	static void Main() {
		Parts p = new Parts();
		p.Method1();
		p.Method2();
		p.Method3();
		p.Method4();
		Console.ReadLine();
	}
}