C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
Partial Method Example
A tutorial example is provided on how to write the partial method definition in one partial class and the partial method signature in other partial classes.
To verify our understanding of partial methods, let's rewrite the MyWindow class from two partial classes presented in the previous tutorial with partial methods:
The first partial class in the first source file, PartialMethod1.cs, includes partial method signatures of two event handler methods:
// PartialMethod2.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); } partial void yesButton_Click(object sender, RoutedEventArgs e); partial void noButton_Click(object sender, RoutedEventArgs e); }
The second partial class in the second source file, PartialMethod2.cs, includes partial method definitions of two event handler methods:
// PartialMethod2.cs // Copyright (c) 2016 HerongYang.com. All Rights Reserved. using System.Windows; using System.Windows.Controls; public partial class MyWindow : Window { partial void yesButton_Click(object sender, RoutedEventArgs e) { myLabel.Content = "Yes clicked"; } partial 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()); } }
The MSBuild project file, PartialMethod.proj, is shown below:
<!-- PartialMethod.proj // Copyright (c) 2016 HerongYang.com. All Rights Reserved. --> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <AssemblyName>PartialMethod</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="PartialMethod1.cs" /> <Compile Include="PartialMethod2.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>
Run "msbuild", we will get the executable program file, PartialMethod.exe.
Table of Contents
Logical Expressions and Conditional Statements
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation