Home > .NET Framework > Partial Types in .NET 2.0

Partial Types in .NET 2.0

Partial types is a new feature in .NET 2.0
They allow splitting the definition of a class / interface / struct into different files or different parts in the same file.
meaning,
 
MyForm.Designer.cs
public partial class MyForm {
// …
// Designer generated code
// …
}
 
MyForm.cs
public partial class MyForm {
// …
// User Code
// …
}
 
Here we are defining a C# class in two different files MyForm.designer.cs and MyForm.cs
 
Benefits of partial types:
  • Very large classes (where it’s cumbersome to navigate with an editor through a single file). Though the first question to ask is why the hell do you have such a large class ;-).
  • Allowing multiple developers to work on a single class, without the need for later merging files in source control.
  • Easing the writing of automatic code generators, such as visual designers. Code generators have had a hard time trying to manage the generated code when it was placed in the human-written code. Passive code generators generate code only once and do not think about modifying / updating the generated code. Active code generators on the other hand are more realistic, assume the need to modify the generated code. If you have to add your own code on top of the generated code, then it’s pain. I often faced this issue when i had to modify the VS.NET(wsdl.exe actually) generated proxy code for web service clients. My changes will be lost if I do a update web reference in VS.NET. The windows forms designer generated code does some active generation using regions. Partial types is an elegant way of doing active code generation.
  • A class does not have access to the private members of another class. However, a nested class can access any part of the nesting class. By writing the unit test classes, occasionally, as a nested class is beneficial. But,keeping my code to be tested and a test code for it in the same file has two problems. My file becomes larger and it is also harder to exclude the tests from a build. Partial classes can help here. It allows me to keep the class in one file and its nested test class(es) in separate file(s). Via ‘The Agile Developer’ Venky.
Some rules:
  • All the partial definitions must be qualified with the partial keyword.
  • The whole class can inherit from only one class. This can be speicified in either one of the partial definitions or all of them. If inheritance is specified in more than one partial definition then it has to be from the same class.
  • Each parital definition can add(implement) one or more interfaces.
  • If any of the partial definitions are declared abstract, then the entire type is considered abstract.
  • If any of the partial definitions are declared sealed, then the entire type is considered sealed.
  • All of the partial definitions must be available at compile time to form the final type.
  • All the partial definitions must have the same accessibility, such as public, private, and so on.
Collected from / Credits to:
Advertisements
Categories: .NET Framework
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: