Partial Classes in C#

In .Net 2.0 it is possible to split the definition of a class or a struct, or an interface over two or more source files with help of “Partial” keyword. There are several situations when splitting a class definition is desirable. Each source file in the project / application contains a section of the class definition, and all parts are combined when the application is compiled.
In following cases using partial for splitting is useful.

  • When more than one developer can simultaneously write the code for the class, while working on large projects.
  • When working on automatically generated .Net source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file ( eg. form1.designer.cs in case of form1.cs) created by Visual Studio.

E.g. . . .

public partial class PartialOne
{
public void First ()
{
}
}

public partial class PartialOne
{
public void Second ()
{
}
}

At the compile time it will be treated as

public class PartialOne
{
public void First() {}
public void Second() {}
}

Within the namespace, we can declare class, struct or interface as partial, when we use this then all parts must use partial keyword. All parts which are declared as partial ( such as in above case First() & Second() ) must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private etc…

If we declared any of the part ( means either of First() or Second() ) then the entire type is considered as abstract , Same is the case with the sealed , What it means that both the parts are required to be of the same attribute. Any class, struct, or interface members declared in a partial definition are available to all of the other parts. The final type is the combination of all the parts at compile time.

Eg .
[System.Abcd]
partial class Example { }

[System.Defg]
partial class Example { }

At Complile time Complier will treat it as

[System.Abcd]
[System.Defg]
class Example()

In the similar way XML comments, interfaces, generic-type parameter attributes, class attributes, members are merged together from all partial parts.

Nested partial types are allowed in partial-type definitions

partial class OuterPartial
{
partial class InnerNestedPartial
{

}
}

The partial modifier can only appear immediately before the keywords class, struct, or interface.

Restrictions while using Partial :

  • The partial class cannot be applied to an enumeration or a delegate, although a delegate is really a class. Or
  • The partial modifier is not available on delegate or enumeration declarations
  • In other words All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.
  • If you define two classes with the same name in different namespaces, the result is a different class, even if each class uses the partial keyword. For multiple parts to be part of the same whole, each part must reside in the same namespace.

There are three advantages according to MS and Internet :-

  1. If a class is really large, break it into multiple files so more than one person can work on it at a time ( Ideal in case of large project , more the one developer need to work on same class)
  2. If a class implements multiple interfaces, put the methods that implement each interface into a separate file.
  3. When the IDE generates code, it puts the code into a separate file so you don’t see it or accidentally modify it.

But Avoid Partial!!!!!!!!!!!!!

  • Avoid Partial for / to breaking large class files in to multiple class files / to organize your class file, due to partial We will not entirely able to get clear picture of class what it consists of and other details or example, you won’t see all the internal fields. If a field needs updating, and it’s not referenced in the portion of the class you’re editing, you’ll never even realize that you are breaking the class. This might creates a problem while support phase of entire product comes.
  • Partial Works Gr8, if it is compiler created the only safe reason to use partial. When the compiler adds partial to your class, it does so to hide the code that it generates. While there are other solutions to handling generated code, this is the one that Microsoft chose, and it works fine.
System Generated Partial Classes
System Generated Partial Classes


Detail Ref

Note: In VB.net, since the partial keyword is optional, you can really confuse someone reading the code because they may not even realize that other code exists for this class. If you work in VB.net, be aware that your entire class may not be in the file you’re looking at.

Advertisements

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