Thursday, April 3, 2008

Partial Classes

Partial classes allow you to break out a class into multiple pieces, usually this is done in multiple files. Why would you want to do this? Well, have you ever seen a really cluttered class that is about 1,000 lines long? Have you ever opened up a Windows Form that had so many controls and many events on those controls that the code file was incredibly long and you spent a lot of time scrolling?

That is why.

It also allows Microsoft to separate out all their auto-generated code so you never have to see it again.

Before:
// MyClass.cs
public class MyClass 
{
      // Line 1
      public void DoWorkHere() { }

      public void DoWorkThere() { }
      // Line 1,026 
}
After:
// MyClass.DoWorkHere.cs
public partial class MyClass 
{
      // Line 1
      public void DoWorkHere() { }
      // Line 513 
} 

// MyClass.DoWorkThere.cs
public partial class MyClass 
{
      // Line 1
      public void DoWorkThere() { } 
     // Line 513 
}
More Info:MSDN: Partial Classes