Partial classes and partial methods are two programming language features of .NET programming languages that make it possible for developers to extend and enhance auto-generated code. In a nutshell, partial classes allow for a single class’s members to be divided among multiple source code files. At compile-time these multiple files get combined into a single class as if the class’s members had all been specified in a single file. Partial methods are methods defined in a partial class that are (optionally) divided across two files. With partial methods one file contains the method signature – the method name, its return type, and its input parameters – while the body is (optionally) defined in a separate file. If the partial method’s body is not defined then the compiler automatically removes the partial method signature and all calls to the method at compile-time. Partial classes and partial methods are most commonly used in auto-generated code. The framework or tool that is auto-generating the code can create the auto-generated classes as partial classes. If the developer using the auto-generated code wants to extend the functionality of the class by adding new methods or properties she can do so by creating a new partial class file and putting her additions there. By having these additions in a separate file there’s no risk of the tool overwriting the developer’s changes when regenerating the code. Regardless of whether you know the ins and outs of partial classes you use them every time you create an ASP.NET web page using a code-behind class. And if you routinely use auto-generated libraries, like LINQ to SQL or Typed DataSets, then it behooves you to be familiar with both partial classes and partial methods, as they offer opportunities for safely extending the functionality of auto-generated code. Read on to learn more about how these features work and how you can put them to work

Share and Enjoy:
  • Digg
  • Google
  • del.icio.us
  • Technorati
  • Facebook
  • MySpace
  • TwitThis
  • Blogsvine
  • description
  • E-mail this story to a friend!
  • Ping.fm
  • Print this article!
  • Slashdot
  • Yahoo! Buzz