Repository navigation
JA 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 Learn。
ClassicUO — Bad Newbie & C# Scripts IDE