New in .NET 10.0 [8]: New features for partial classes in C# 14.0
In C# 14.0, there are now also partial constructors and partial events.
(Image: Pincasso/Shutterstock)
1 min. read
By
- Dr. Holger Schwichtenberg
C# has supported partial classes since version 2.0 and partial methods since version 3.0. In 2024, C# 13.0 introduced partial properties and indexers.
In C# 14.0, developers can also apply the partial keyword to constructors and events in C# classes.
The following code shows part 1 of the class:
public partial class Person
{
public partial int ID { get; set; } // Partial Property
public partial string Name { get; set; } // Partial Property
public partial void Print(); // Partial Method
public partial Person(int id); // NEU: Partial Constructor
public static partial event Action<int, string> PersonCreated; // NEU: Partial Event
}
Videos by heise
Part 2 of the class can be found in the following code:
// Teil 2 der Klasse
public partial class Person
{
private int _ID;
public partial int ID
{
get { return _ID; }
set { _ID = value; }
}
private string _Name = "";
public partial string Name
{
get { return _Name; }
set { _Name = value; }
}
// NEU: Partial Constructor
public partial Person(int id)
{
this.ID = id;
Person._PersonCreated?.Invoke(this.ID, this.Name);
}
public partial void Print()
{
Console.WriteLine($"Person-ID: {this.ID} Name: {this.Name}");
}
private static Action<int, string> _PersonCreated;
// NEU: Partial Event: Implementierung des Partial Events muss add- und remove-Block besitzen
public static partial event Action<int, string> PersonCreated
{
add
{
Console.WriteLine("Handler fĂĽr PersonCreated hinzugefĂĽgt.");
_PersonCreated += value;
}
remove
{
Console.WriteLine("Handler fĂĽr PersonCreated entfernt.");
_PersonCreated -= value;
}
}
}
(dahe)