Partial Classes

I'm really liking this idea of Partial Classes that is coming in .Net 2.0.

When I first heard about it, I thought “now that is going to be confusing“. But I've been playing with it in Whidbey and I think it's going to be cool. Both VB.NET (When do we get to stop calling it VB.NET and just call it VB again)  and C# allow you to split your class across two files. Although the syntax is slightly different.

C# - Each part of the class has the same definition as seen below

  • public partial class MyClass
  • public partial class MyClass

VB.NET - There is a main class and all others expand on that like this

  • Public Class MyClass
  • Expands Class MyClass

For those who are wondering, The two files must reside in the same assembly. They are actually combined at compile time.

I have been thinking about how useful this will be. Here are some of the uses I can see for Partial Classes.

  1. That section of code generated by the designer that you shouldn't touch, can go in another file (And in Whidbey it will)
  2. Extending a Typed Dataset or any other auto generated class (This is the same basic reason as number 1)
  3. Code Behind, I assume the ASP.NET team will use partial classes to implement code behind
  4. Multiple developers, two developers can work on different areas of a class at the same time without worrying about merging changes. Therefore it could be used to logically divide a class, think of it like regions on steroids.

What else? I'm curious to hear about others peoples ideas for Partial Classes.