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) 2016 HerongYang.com. 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 =
         System.Windows.VerticalAlignment.Top;
      yesButton.Click += new RoutedEventHandler(yesButton_Click);
      myGrid.Children.Add(yesButton);

      Button noButton = new Button();
      noButton.Content = "No";
      noButton.Margin = new Thickness(50, 50, 50, 0);
      noButton.Height = 30;
      noButton.VerticalAlignment =
         System.Windows.VerticalAlignment.Top;
      noButton.Click += new RoutedEventHandler(noButton_Click);
      myGrid.Children.Add(noButton);

      myLabel = new Label();
      myLabel.Margin = new Thickness(50,90,50,0);
      myGrid.Children.Add(myLabel);
   }
}

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

// PartialClass2.cs
// Copyright (c) 2016 HerongYang.com. 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";
   }

   [System.STAThread]
   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) 2016 HerongYang.com. All Rights Reserved.
-->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
     <AssemblyName>PartialClass</AssemblyName>
     <NET>C:\windows\Microsoft.NET\Framework\v4.0.30319</NET>
     <REF1>C:\Program Files\Reference Assemblies</REF1>
     <REF2>\Microsoft\Framework\.NETFramework\v4.6.1</REF2>
     <REF>$(REF1)$(REF2)</REF>
  </PropertyGroup>
  <ItemGroup>
    <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" />
  </ItemGroup>
  <Target Name="Build">
    <Csc ToolPath="$(NET)" Sources="@(Compile)"
       References="@(Reference)"
       OutputAssembly="$(AssemblyName).exe"/>
  </Target>
</Project>

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).
Build:
  C:\windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
     \Framework\.NETFramework\v4.6.1\PresentationFramework.dll"
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
     \Framework\.NETFramework\v4.6.1\PresentationCore.dll"
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
     \Framework\.NETFramework\v4.6.1\System.Xaml.dll"
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
     \Framework\.NETFramework\v4.6.1\WindowsBase.dll"
  /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

 Archived Tutorials

 References

 Full Version in PDF/ePUB