Repository navigation
FR CSharp Keyword partial member
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
Un membre partiel a une déclaration et une implémentation distinctes à l'intérieur du type partiel. La mise en œuvre est obligatoire pour la méthode et la propriété publiques données ; sa signature doit correspondre à l'annonce. Les propriétés partielles sont prises en charge à partir de C# 13. Le projet compile toutes les parties ensemble.
Enregistrez cet exemple séparément dans Example.cs. Exécutez-le indépendamment des autres exemples de cette page.
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; }Résultat:
7
public partial string Format(string name, int count);
public partial string Name { get; set; }Fichier Example.csproj :
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>14.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>Fichier Program.cs :
using System;
internal static class Program
{
public static void Main()
{
var formatter = new Formatter();
Console.WriteLine(formatter.Format("Bandages", 15));
}
}Fichier Formatter.Contract.cs :
public partial class Formatter
{
public partial string Format(string name, int count);
}Fichier Formatter.Implementation.cs :
public partial class Formatter
{
public partial string Format(string name, int count) => $"{name}: {count}";
}Résultat:
Bandages: 15
Fichier Example.csproj :
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>14.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>Fichier Program.cs :
using System;
internal static class Program
{
public static void Main()
{
var settings = new Settings { Name = " Traveller " };
Console.WriteLine($"[{settings.Name}]");
}
}Fichier Settings.Contract.cs :
public partial class Settings
{
public partial string Name { get; set; }
}Fichier Settings.Implementation.cs :
public partial class Settings
{
private string name = "";
public partial string Name
{
get => name;
set => name = value.Trim();
}
}Résultat:
[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(); } }Source officielle : Microsoft Apprendre.
ClassicUO — Bad Newbie & C# Scripts IDE