由這段話可以一窺 partial types 的原始構想:

Host: Anders (Microsoft) Q: What’s the aim of partial types in C#? A: Two reasons. First, people often ask us for include files. When we dig a little deeper, it turns out they want to split large classes into multiple files. Partial types will allow you to do that, but in a structured fashion. Second, partial classes solve the round trip problem with auto generated code. The code generator can create one file and the user’s additions can be placed in another file. When code is regenerated, the user’s changes aren’t lost.

這是 C# 2.0 specification 對 partial types 的解釋:

Partial types allow classes, structs, and interfaces to be broken into multiple pieces stored in different source files for easier development and maintenance. Additionally, partial types allow separation of machine-generated and user-written parts of types so that it is easier to augment code generated by a tool.

依我的看法,第二個理由『可以分離 machine-generated code 及 user-written code』才是 partial types 的真正的價值所在,而分離這兩者又是 XAML 及新版的 ASP.NET 必須要有的功能,正因為如此 partial types 也就順理成章地成為 C# 2.0 的新規格。以 XAML 為例,XAML 的核心精神就是『分離 presentation 及 behavior』。 Presentation 指的是使用者界面,通常是由美工人員負責撰寫如下的 XAML 檔來達成〈很像寫 HTML 網頁〉:

<Canvas ID="root" xmlns="http://schemas.microsoft.com/2003/xaml" xmlns:def="Definition">
  <Button Click="Button_Click">Click Me!</Button>
</Canvas>

而 Behavior 指的是 UI 背後的行為邏輯,這部份通常是由程序員使用 C# 或其他的 .NET Language 來撰寫:

using System;
using MSAvalon.Windows;
using MSAvalon.Windows.Controls;
using MSAvalon.Windows.Media;

namespace Button {
  public class Default : Panel {
    // Event handler
    void Button_Click(object sender, MSAvalon.Windows.Controls.ClickEventArgs e) {
      btn1.Background = MSAvalon.Windows.Media.Brushes.Red;
    }
  }
}

將二者分離對開發程序的影響就是可以讓美工人員跟程序員可以獨立作業。在這個例子中,machine-generated code 指的就是由 XAML 產生的 C# code,user-written code 就是由程序員自行撰寫的 event handlers,最後二者藉由 partial types 的機制合併而成為單一的 class 來執行,很酷不是嗎?