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 & files is a free C# Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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());
  }
}

Partial methods

Partial methods are compile-time hooks: if not implemented, the call is removed. In C# 6 they must be void and are private.

using System;

namespace Demo
{
  public partial class Processor
  {
    // Partial method: implicitly private, must return void in C# 6
    partial void OnProcessed(string item);

    public void Run(string item)
    {
      // do work...
      OnProcessed(item); // call hook (removed by compiler if not implemented)
    }
  }

  public partial class Processor
  {
    // Implementation lives in another part (often another file)
    partial void OnProcessed(string item)
    {
      Console.WriteLine("Processed: " + item);
    }
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Demo.Processor p = new Demo.Processor();
    p.Run("report");
  }
}

When to use partial

Use cases:

  • Designer or generated code in one file; your safe edits in another.
  • Large types split by feature: data vs behavior.
  • Optional hooks via partial methods.

Partial struct

You can also split structs with partial; the result is one struct type at compile time.

using System;

namespace Demo
{
  public partial struct Pair
  {
    public int A;
  }

  public partial struct Pair
  {
    public int B;
    public int Sum() { return A + B; }
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Demo.Pair p = new Demo.Pair();
    p.A = 2; p.B = 3;
    Console.WriteLine("Sum = " + p.Sum());
  }
}

Partial rules & tips

Tips:

  • All parts must use the same namespace, name, and access modifier.
  • Keep each file focused (generated vs manual).
  • In C# 6, partial methods are void and private; keep them tiny.

Partial class purpose

Quick check: What is a practical use of a partial class in C# 6?

Recap

Recap: Use partial to split types across files and add partial methods for optional hooks. Keep parts in the same namespace/name and let the compiler merge them.

Frequently asked questions

Is the “Partial types & files” lesson free?

Yes — the full text of “Partial types & files” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Partial types & files”?

Split a type across files with partial classes/structs and use partial methods to hook custom logic into generated code. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Partial types & files” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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