Repository navigation
CSharp Keyword partial member
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
Довідник C# · Практичне керівництво
Частковий член має окреме оголошення та реалізацію всередині partial-типу. Для наведеного public-методу та властивості реалізація обов’язкова; її сигнатура має збігатися з оголошенням. Часткові властивості підтримуються починаючи з C# 13. Проєкт компілює всі частини разом.
Збережіть цей приклад окремо як 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]
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; } }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.
ClassicUO — Bad Newbie & C# Scripts IDE