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

Last update: 2015.

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

 References

 PDF Printing Version