0Pricing
C# Academy · Lesson

Partial types & files

Split a type across files with partial classes/structs and use partial methods to hook custom logic into generated code.

Partial types overview

Goal: Split large types cleanly.

  • partial class/struct: same namespace + name
  • Usually spread across multiple files
  • partial methods: optional hooks inside partial types

Partial class parts

A partial class can be declared in multiple parts. The compiler merges them into one type.

using System;

namespace Demo
{
  // Part 1: data
  public partial class Person
  {
    public string Name { get; private set; }
    public Person(string name) { Name = name; }
  }

  // Part 2: behavior
  public partial class Person
  {
    public string Greet() { return "Hi " + Name; }
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Demo.Person p = new Demo.Person("Ada");
    Console.WriteLine(p.Greet());
  }
}

All lessons in this course

  1. public/internal/protected/private (with note on file)
  2. Namespaces, using directives, global usings (note)
  3. Partial types & files
← Back to C# Academy