C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
What Is Partial Class?
A quick introduction is provided on 'partial class', which declares an incomplete definition of a class. C# compile will merge multiple partial classes from multiple source files to form a complete class definition.
What Is Partial Class? A partial class is an incomplete definition of a class. C# supports partial class to allow developers to write a single class in multiple source files with each source file contains a partial definition of the class. Partial class definitions from multiple source files will be merged together by C# compile to form a complete class definition.
There several situations where partial class can be useful:
When coding partial class, we need to follow some rules, so that C# compile can merge multiple partial class definitions into a complete class:
For example, if we want to define class A in 2 source files: A1.cs and A2.cs, we can write the following in A1.cs:
// A1.cs
public partial class A {
private int size;
public static void Main() {
...
}
void method1() {
...
}
}
And write the following in A2.cs:
// A2.cs
public partial class A {
private float price;
void method2() {
...
}
void method3() {
...
}
}
Then we can compile them together to generate the executable program, A.exe:
C:\herong>csc /out:A.exe A1.cs A2.cs
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