Skip to content

CSharp Keyword partial member

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: partial — частковий член

Довідник C# · Практичне керівництво

Частковий член має окреме оголошення та реалізацію всередині partial-типу. Для наведеного public-методу та властивості реалізація обов’язкова; її сигнатура має збігатися з оголошенням. Часткові властивості підтримуються починаючи з C# 13. Проєкт компілює всі частини разом.

Простий приклад (Example.cs)

Збережіть цей приклад окремо як Example.cs. Запускайте його незалежно від інших прикладів на сторінці.

Example.cs:

// PROJECT-EXAMPLE
System.Console.WriteLine(new Item().Read());
partial class Item { public partial int Read(); }
partial class Item { public partial int Read() => 7; }

Результат:

7

Синтаксис

public partial string Format(string name, int count);
public partial string Name { get; set; }

Оголошення й реалізація public partial методу

Файл Example.csproj:

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

</Project>

Файл Program.cs:

using System;
internal static class Program
{
    public static void Main()
    {
        var formatter = new Formatter();
        Console.WriteLine(formatter.Format("Bandages", 15));
    }
}

Файл Formatter.Contract.cs:

public partial class Formatter
{
    public partial string Format(string name, int count);
}

Файл Formatter.Implementation.cs:

public partial class Formatter
{
    public partial string Format(string name, int count) => $"{name}: {count}";
}

Результат:

Bandages: 15

Часткова властивість із перевіркою присвоєння

Файл Example.csproj:

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

</Project>

Файл Program.cs:

using System;
internal static class Program
{
    public static void Main()
    {
        var settings = new Settings { Name = "  Traveller  " };
        Console.WriteLine($"[{settings.Name}]");
    }
}

Файл Settings.Contract.cs:

public partial class Settings
{
    public partial string Name { get; set; }
}

Файл Settings.Implementation.cs:

public partial class Settings
{
    private string name = "";
    public partial string Name
    {
        get => name;
        set => name = value.Trim();
    }
}

Результат:

[Traveller]

Приклад 3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Counter value = new(5);
        Console.WriteLine(value.Value);
    }
}
public partial class Counter { public int Value { get; } public partial Counter(int value); }
public partial class Counter { public partial Counter(int value) { Value = value; } }

Приклад 4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Signal value = new();
        value.Fired += () => Console.WriteLine("Fired");
        value.Fire();
    }
}
public partial class Signal { public partial event Action Fired; }
public partial class Signal { private Action? handlers; public partial event Action Fired { add { handlers += value; } remove { handlers -= value; } } public void Fire() { handlers?.Invoke(); } }

Офіційне джерело: Microsoft Learn.

Clone this wiki locally