Repository navigation
KO CSharp Keyword partial type
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
부분은 컴파일 타임에 동일한 유형의 여러 선언을 결합합니다. 부품은 동일한 어셈블리 및 네임스페이스에 속해야 하며 일관된 유형 이름을 가져야 합니다. 이렇게 하면 소스 파일 간의 데이터와 동작을 분리할 수 있습니다. 그것은 몇 가지 별개의 유형이 아닙니다.
이 예제를 Example.cs로 따로 저장하고 이 페이지의 다른 예제와 독립적으로 실행하세요.
Example.cs:
// PROJECT-EXAMPLE
System.Console.WriteLine(new Item().Name);
partial class Item { public string Name => GetName(); }
partial class Item { private string GetName() => "Ore"; }결과:
Ore
public partial class Label { }
public partial struct Rectangle { }파일 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 label = new Label { Name = "Sensor", Number = 12 };
Console.WriteLine(label.Format());
}
}파일 Label.Data.cs:
public partial class Label
{
public string Name { get; init; } = "";
public int Number { get; init; }
}파일 Label.Display.cs:
public partial class Label
{
public string Format() => $"{Name} #{Number}";
}결과:
Sensor #12
파일 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 size = new Rectangle { Width = 8, Height = 6 };
Console.WriteLine(size.Area);
}
}파일 Rectangle.Size.cs:
public partial struct Rectangle
{
public int Width { get; init; }
public int Height { get; init; }
}파일 Rectangle.Area.cs:
public partial struct Rectangle
{
public int Area => Width * Height;
}결과:
48
공식 출처: Microsoft 학습.
ClassicUO — Bad Newbie & C# Scripts IDE