able to put part of the class in one file, and put the other part (or more parts) in
other files? In C#, this is quite possible!
By using the ‘partial’ class modifier, you can define a class across multiple files. The compiled class merges the various partial source files into a single compiled class. So, for instance, if you have the source code
public partial class A
{
public void method1()
{...}
}
and somewhere else you have the source code
public partial class A
{
public void method2()
{...}
}
then the compiled object will exhibit both method1 and method2. Note that it’s important that the various aspects of the declaration like modifiers, type parameters, etc. all match up across the multiple partial declarations.Your partial classes have to be within the same namespace, so if you are to be putting them in seperate files, you must have it contained in the same namespace.
You can do this by typing
namespace NameSpaceNameHere
{
//Enter Partial Class Here
}
for instance when you open a new Console Application, the default namespace is normallyConsoleApplication1 that is if you haven't changed your project name.


Sign In
Create Account


Back to top









