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.

listen Print view
Guide with C#

(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.

The Dotnet Doctor – Holger Schwichtenberg
Der Dotnet-Doktor – Holger Schwichtenberg

Dr. Holger Schwichtenberg is the technical director of the expert network www.IT-Visions.de, which supports numerous medium-sized and large companies with consulting and training services as well as software development, drawing on the expertise of 53 renowned experts. Thanks to his appearances at numerous national and international conferences, as well as more than 90 specialist books and over 1,500 specialist articles, Holger Schwichtenberg is one of the best-known experts for .NET and web technologies in Germany.

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)

Don't miss any news – follow us on Facebook, LinkedIn or Mastodon.

This article was originally published in German. It was translated with technical assistance and editorially reviewed before publication.