Skip to content

EN CSharp Keyword partial type

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

C#: partial is a partial type

Handbook C# · Practical Guide

partial combines multiple declarations of the same type at compile time. The parts must belong to the same assembly and namespace and have a consistent type name. This way you can separate data and behavior between source files; it is not a few separate types.

Simple example (Example.cs)

Save this example separately as Example.cs. Run it independently of the other examples on this page.

Example.cs:

// PROJECT-EXAMPLE
System.Console.WriteLine(new Item().Name);
partial class Item { public string Name => GetName(); }
partial class Item { private string GetName() => "Ore"; }

Result:

Ore

Syntax

public partial class Label { }
public partial struct Rectangle { }

A class with data and formatting in different files

File Example.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>14.0</LangVersion>
    <Nullable>enable</Nullable>
    
  </PropertyGroup>

</Project>

File Program.cs:

using System;
internal static class Program
{
    public static void Main()
    {
        var label = new Label { Name = "Sensor", Number = 12 };
        Console.WriteLine(label.Format());
    }
}

File Label.Data.cs:

public partial class Label
{
    public string Name { get; init; } = "";
    public int Number { get; init; }
}

File Label.Display.cs:

public partial class Label
{
    public string Format() => $"{Name} #{Number}";
}

Result:

Sensor #12

Partial structure with dimensions and area calculation

File Example.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>14.0</LangVersion>
    <Nullable>enable</Nullable>
    
  </PropertyGroup>

</Project>

File Program.cs:

using System;
internal static class Program
{
    public static void Main()
    {
        var size = new Rectangle { Width = 8, Height = 6 };
        Console.WriteLine(size.Area);
    }
}

File Rectangle.Size.cs:

public partial struct Rectangle
{
    public int Width { get; init; }
    public int Height { get; init; }
}

File Rectangle.Area.cs:

public partial struct Rectangle
{
    public int Area => Width * Height;
}

Result:

48

Official source: Microsoft Learn.

Clone this wiki locally