Skip to content

KO CSharp Keyword partial member

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: partial는 부분 용어입니다.

핸드북 C# · 실용 가이드

부분 멤버에는 부분 유형 내에 별도의 선언과 구현이 있습니다. 지정된 공용 메서드 및 속성에 대한 구현은 필수입니다. 그녀의 서명이 광고와 일치해야 합니다. 부분 속성은 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; }

공개 부분 메서드 선언 및 구현

파일 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 학습.

Clone this wiki locally