Repository navigation
RU 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