.NET framework 2.0 quickie - partial classes - Ed's Blog
in

dotnet.org.za

South African .NET Developer Portal

Ed's Blog

Object reference not set to an instance of an object

.NET framework 2.0 quickie - partial classes

[Update] - posted this up on sadeveloper.net

When I started looking at the Visual Studio 2005 Beta, I listed a couple of toppings that I wanted to check out. Then I thought it might be a good idea write up a little “article” on each topic, for my own reference, and who knows, maybe for someone else's as well. So here goes the first one:

What it's all about
The idea is to split normal classes into several declarations, these declarations will typically reside in different files. During compilation, these declarations(partial classes) are combined into one and compiled as if there was only one class definition.

Examle

[Employee.cs]
public partial class Employee
{
  string _name;
  int _empNo;

  public Employee(int empNo,string name)
  {
    _empNo = empNo;
    _name = name;
  }

}

[EmployeeProperties.cs]
public partial class Employee
{
  public int EmpNo
  {
    get
    {
      return _empNo;
    }
   
set
    {
      _empNo =
value;
    }
  }

  public string Name
  {
    get
    {
      return _name;
    }
    set
   {
      _name =
value;
   }
 }
}

Rules

  • All classes that will be joined together must be marked with the “partial” keyword. This will prevent someone from just adding a partial class to your existing class and messing around with the internal workings
  • Of course members (fields,methods, properties etc.) can only be defined once throught all partial classes with the same name.
  • All the partial definitions must be in the same namespace(also obvious) and must be contained in the same module(exe or dll).
  • If you're planning to add partial classes in future, you can use the “partial” keyword, even though you only have one definition. There is no rule that states that you have to have at least 2 partial definitions to be able to use “partial” on a class.

Pro's

  • Using partial classes we can devide one class into several files, allowing several developers to work on the same class at once while still having the full benefit of source control
  • Allows you to split the functionality of your classes logically into groups. As in the example you can put all your properties in on file, constructors in another, business logic functions in another, or whatever suites your project best. This aids in avoiding “clutter“, as is demonstrated in the new ASP.NET code behind files.
    Another good idea would be to put all the parts of your class that was generated by a code generator in one file.

Con's

  • The major drawback I see in partial classes is that developers will use it to “extend” classes instead of normal OO mechanism, thus ending up with huge monolithic classes.
  • The other issue is just around locating all the partial classes that contribute to a class. Hopefully Visual Studio or tools like ReShaper will be able to help us quickly navigate between partial classes instead of having to remember in which files they are defined. For now (an at all times really) you can just use a nice naming convention. 

 

 

Published Aug 13 2004, 08:16 AM by eduard
Filed under:

Comments

 

Tallies the techo monkey said:

I don't think this feature adds enough value to even consider using! Maybe I'm not enlightened enough, but this has trouble written all over it. The pro's seem more like con's to me:

"Using partial classes we can devide one class into several files, allowing several developers to work on the same class at once while still having the full benefit of source control " - If you class is big enough to require several developers to work on it at once, then I think you need head back to the drawing board, because not enough effort was spent refactoring the class before coding started.

"Allows you to split the functionality of your classes logically into groups." - Whats wrong with "#Region". It seems to keep my code pretty much uncluttered. All this will lead to is "un-synchronized" code (for lack of better terminology). I update partial class Y with method X. But you didn't know about it so you continue. You suddenly also need a method like X. You don't know I created a partial class for methods used in scenario A (because, face it, we can break our classes into sections for specific scenarios now, huh?). You create your own, and 2 week down the line we suddenly realise we both spend a day doing the same work.

Maybe if you have a process as rigorous as 100 year old tree you may get away with working on the same class without overlapping work or wasting time because of internal class dependencies, but then you'll still not get anything done faster because you spend all you time on process.

I stand corrected (once I actually start using .Net Framework 2.0) but I think this is not the best idea.
August 15, 2004 2:35 PM
 

Eduard Penzhorn said:

Thanks for your feedback!

One thing that I want to emphasize, is that the idea behind the article is to give a quick summary of the new features as I explore them. I'm trying to stay on the fence for now, although I must agree that I'm not entirely convinced myself.
August 16, 2004 7:47 AM
 

Armand du Plessis said:

" don't think this feature adds enough value to even consider using!" While I agree with the majority of the comment that if your class get's so big it needs to split etc etc etc there certainly is cases where partial classes come in very handy. The most mentioned example that comes to mind is auto or tool generated code. If you extend or add to say a data component generated by 2005 you don't want to lose all your code every time you regenerate the component. Just split out all the custom code into a partial class and you can safely regenerate the component as often as you like.
Also just looking at the new asp.net 2.0 code compilation model which would not have been possible without the addition of partial classes to the compiler.
August 16, 2004 10:31 AM
 

Ernst Kuschke said:

I used to burst out in tears every time I lose my code in a designer-generated method like InnitialiseComponent() on a WinForm.
Now this code is in a seperate file, as Armand points out - very useful ;o)
August 16, 2004 4:04 PM
 

TrackBack said:

April 1, 2005 7:06 PM
 

TrackBack said:

April 1, 2005 8:05 PM
 

Eldar (NasdaqNM: ERJR) said:

January 2, 2006 1:43 AM
 

Simran said:

I am not evaluating the pro and cons of partial classes but your article was useful enough for me as I am new to ASP.Net 2.0. Thanks

July 7, 2007 9:18 AM
 

Rafi khan said:

what is partial class and its use?

September 8, 2007 11:36 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Powered by Community Server (Commercial Edition), by Telligent Systems