Partial Class Example

A tutorial example is provided on how to write two source files with two partial classes. When compile them together, two partial classes become a single complete class.

To verify our understanding of partial classes, let's rewrite the MyWindow class from WpfButtonTest.cs file presented earlier in the book into two partial classes:

The first partial class only includes the private property myLabel and the constructor MyWindow() in the first source file, PartialClass1.cs:

// PartialClass1.cs
// Copyright (c) All Rights Reserved.

using System.Windows;
using System.Windows.Controls;
public partial class MyWindow : Window {
   private Label myLabel;
   public MyWindow() {
      Width = 200;
      Height = 200;
      Title = "WPF Button Test";

      Grid myGrid = new Grid();
      Content = myGrid;

      Button yesButton = new Button();
      yesButton.Content = "Yes";
      yesButton.Margin = new Thickness(50, 10, 50, 0);
      yesButton.Height = 30;
      yesButton.VerticalAlignment =
      yesButton.Click += new RoutedEventHandler(yesButton_Click);

      Button noButton = new Button();
      noButton.Content = "No";
      noButton.Margin = new Thickness(50, 50, 50, 0);
      noButton.Height = 30;
      noButton.VerticalAlignment =
      noButton.Click += new RoutedEventHandler(noButton_Click);

      myLabel = new Label();
      myLabel.Margin = new Thickness(50,90,50,0);

The second partial class includes two event handlers and the entry point Main() in the second source file, PartialClass2.cs:

// PartialClass2.cs
// Copyright (c) All Rights Reserved.

using System.Windows;
using System.Windows.Controls;
public partial class MyWindow : Window {

   void yesButton_Click(object sender, RoutedEventArgs e) {
      myLabel.Content = "Yes clicked";
   void noButton_Click(object sender, RoutedEventArgs e) {
      myLabel.Content = "No clicked";

   public static void Main() {
      Application app = new Application();
      app.Run(new MyWindow());

Of course, we can compile them with a MSBuild project file, PartialClass.proj:

<!-- PartialClass.proj
// Copyright (c) All Rights Reserved.
<Project xmlns="">
     <REF1>C:\Program Files\Reference Assemblies</REF1>
    <Compile Include="PartialClass1.cs" />
    <Compile Include="PartialClass2.cs" />
    <Reference Include="$(REF)\PresentationFramework.dll" />
    <Reference Include="$(REF)\PresentationCore.dll" />
    <Reference Include="$(REF)\System.Xaml.dll" />
    <Reference Include="$(REF)\WindowsBase.dll" />
  <Target Name="Build">
    <Csc ToolPath="$(NET)" Sources="@(Compile)"

Here is the compilation result:

C:\herong>set "NET=\windows\Microsoft.NET\Framework\v4.0.30319"

C:\herong>%NET%\msbuild PartialClass.proj
Microsoft (R) Build Engine version 4.6.1055.0
[Microsoft .NET Framework, version 4.0.30319.42000]
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 8:42:22 PM.
Project "C:\herong\PartialClass.proj" on node 1 (default targets).
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
  /out:PartialClass.exe PartialClass1.cs PartialClass2.cs

Done Building Project "C:\herong\PartialClass.proj" (default targets).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.42

Now we can run the executable program file: PartialClass.exe.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

Partial Classes and Partial Methods

 What Is Partial Class?

Partial Class Example

 What Is Partial Method?

 Partial Method Example

 Outdated Tutorials


 Full Version in PDF/ePUB