Partial classes can be used to split the class definition in multiple files and then use the methods from all of those files as single class. For example you have a class Module that contains all the properties and methods regarding the Module functionality of your project. You can split the functionality into two different classes i.e. ModuleMain.cs, ModuleExtended.cs
Here is the sample code:
file: ModuleMain.cs
code:
public partial class Module
{
//constructor
public Module() {}
//some methods go here
public bool MainMethod()
{
return true;
}
}
file: ModuleExtended.css
code:
public partial class Module
{
//extended methods go here
public bool ExtendedMethod()
{
return true;
}
}
Now, when you'll access the class Module from your code, you'll see that the intellisense will show all the methods from both of these files.
Sometimes, this feature comes very handy. For example, I wrote a utility to generate code for asp.net application. But the problem was that the classes generated by the utility would overwrite the additional methods added manually. So, I split the functionality using partial classes and separated the generated and manual code into two different files. I hope this feature will help you too.
No comments:
Post a Comment