.NET 9.0 Preview 6 brings some long-awaited features

0
27
.NET 9.0 Preview 6 brings some long-awaited features


Microsoft released the sixth preview version of .NET 9.0 on the night of Tuesday to Wednesday available for downloadFor development with .NET 9.0 Preview 6 you need the current preview version of Visual Studio 2022 17.11, which is also available at the same time Updated to “Preview 3”.. Alternatively, you can use Visual Studio Code Use with the C# Dev Kit,

Advertisement


One of the most important new features in .NET 9.0 Preview 6 is partial properties for the thirteenth language version of C#, which comes with .NET 9.0. Many have been waiting for this language feature since the introduction of partial methods in C# 3.0. Key Words partial Even been around for classes since C# 2.0.

With partial classes you can split the program code of the same class into multiple files – without using inheritance. This is not only useful for greater clarity in large classes, but it is mainly used when one part of the class is automatically generated and the other part of the class is written manually. This approach is used in .NET, for example, in GUI libraries such as ASP.NET WebForms and Blazor, in reverse engineering of databases with Entity Framework and Entity Framework Core, and in source generators for regular expressions and JSON serialization, among other things.

In C# 13.0, developers can also separate property definitions and their implementation into two files. Both parts must implement the same combination of getters and setters with the same visibility. Concrete example: If a property has both a public getter and a public setter in one part of a class, these must also be present and public in the other part. But where one part uses an automatic property, the other part can have an explicit implementation. The following code snippets show an example of a split class with a partial method and a partial property.

The first part of the class with only the definitions of the id and Print() Uses an automatic property:

namespace NET9_Console.CS13;

public partial class PersonWithAutoID
{
  public partial int ID { get; set; }
  public string Name { get; set; }

  public partial void Print();
}

Getters and setters for the id as well as the method in another part of the class Print() Explicitly implemented:

namespace NET9_Console.CS13;

public partial class PersonWithAutoID
{
 int counter = 0;

 private int iD;

 public partial int ID
 {
  get
  {
   if (iD == 0) iD = ++counter;
   return iD;
  }
  set
  {
   if (ID>0) throw new ApplicationException("ID ist bereits gesetzt");
   iD = value;
  }
 }

 public partial void Print()
 {
  Console.WriteLine($"{this.ID}: {this.Name}");
 }
}

The following code uses the Composite class PersonWithAutoID

public class CS13_PartialPropertyDemoClient
{
 public void Run()
 {
  CS13.PersonWithAutoID p = new() { Name = "Holger Schwichtenberg" };
  p.Print(); // 1: Holger Schwichtenberg
  try
  {
   p.ID = 42;
  }
  catch (Exception ex)
  {
   CUI.Error(ex); // System.ApplicationException: ID ist bereits gesetzt
  }
 }
}

Python library for Nvidia’s CUDA-X: faster algebraic calculationsPython library for Nvidia’s CUDA-X: faster algebraic calculations

Microsoft themselves are using this new language feature and in the current preview 6 the source generator for regular expressions allows you to use annotations (GeneratedRegex) Now it can be used not only for methods, but also for properties. The following code example uses the innovation to validate an email address:

public partial class Checker_Alt // Partielle Klasse
{
 (GeneratedRegex(@"\w+((-+.')\w+)*@\w+((-.)\w+)*\.\w+((-.)\w+)*"))
 // Partielle Methode, die dann von SG implementiert wird
 public partial Regex EMailRegEx();
}

public partial class Checker_Neu // Partielle Klasse
{
 (GeneratedRegex(@"\w+((-+.')\w+)*@\w+((-.)\w+)*\.\w+((-.)\w+)*"))
 // Partielle Property mit Getter, der dann von SG implementiert wird
 public partial Regex EMailRegEx { get; }
}

public class FCL9_RegExSourceGenerator
{
 public void Run()
 {
  CUI.Demo(nameof(FCL9_RegExSourceGenerator));
  // Aufruf der partiellen Methode
  Console.WriteLine(new Checker_Alt().EMailRegEx().IsMatch("max@mustermann.de"));
  // Aufruf der partiellen Property
  Console.WriteLine(new Checker_Neu().EMailRegEx.IsMatch("max@mustermann.de"));
 }
}

The source generator introduced in .NET 7.0 creates the getter of the method’s content and partial property when compiled during development. The generated program code can be found in the RegexGenerator.g.cs file in the C:\Users\Username\AppData\Local\Temp\VSGeneratedDocuments\GUID folder.




(Image: Dmytro Vikarchuk/Shutterstock))

In Online Conference BetterCode() .NET 9.0 On November 19, 2024 by iX and dpunkt.verlag, .NET experts from www.IT-Visions.de will present a ready-made version of .NET 9.0 using practical examples. These include the .NET 9.0 SDK, C# 13.0, ASP.NET Core 9.0, Blazor 9.0, Windows Forms 9.0, WPF 9.0, WinUI, innovations in .NET MAUI 9.0 and the integration of artificial intelligence into .NET applications. Program Offers six lectures, one discussion, and six workshops.

There are tickets Available at an introductory price,

This class has been around in .NET for a long time System.Collections.Specialized.OrderedDictionary To store name-value pairs for quickly finding an element using the key. Add()The method expects any two .NET objects. In .NET 9.0, Microsoft finally introduces a generic version of this OrderedDictionary which can be used to fix the type of the key and value:

public void GenericOrderedDictionary()
 {
  CUI.Demo(nameof(GenericOrderedDictionary));

  OrderedDictionary d = new()
  {
   ("www.IT-Visions.de") = 1996,
   ("www.dotnet7.de") = 2022,
   ("www.dotnet8.de") = 2023,
   ("www.dotnet-lexikon.de") = 2000,
  };

  d.Add("www.dotnet9.de", 2024);
  d.RemoveAt(1);
  d.Insert(0, "www.dotnetframework.de", 2000);

  foreach (KeyValuePair entry in d)
  {
   Console.WriteLine(entry);
  }
 }

The list provides the following output:

(www.IT-Visions.de, 1996)
(www.dotnet8.de, 2023)
(www.dotnet-lexikon.de, 2000)
(www.dotnet9.de, 2024)

.NET 9.0 since Preview 6 provides another new set of classes: ReadOnlySet. As usual with sets, duplicates are not allowed. Unlike HashSet contents of ReadOnlySet Do not change.

for the construction of ReadOnlySet is a set as an object with an interface ISet For example, it is necessary HashSet Like in the next code example. ReadOnlySet Completes the read-only set in .NET. There were even earlier ReadOnlyCollection for everything, was IList offers, and ReadOnlyDictionary For IDictionary,

public void ReadOnlySet()
{
 CUI.Demo(nameof(ReadOnlySet));

 CUI.H1("HashSet erlaubt Hinzufügen/Löschen");

 HashSet set1 = new()
 {
  // alle obigen URLs hinzufügen
  "www.IT-Visions.de",
  "www.dotnet7.de",
  "www.dotnet8.de",
  "www.dotnet9.de",
  "www.dotnettraining.de",
 };

 var r = set1.Add("www.dotnet9.de");
 Console.WriteLine(r ? "URL neu" : "URL schon vorhanden");
 set1.Remove("www.dotnet7.de");

 foreach (var url in set1)
 {
  Console.WriteLine(url);
 }

 CUI.H1("ReadOnlySet erlaubt KEIN Hinzufügen/Löschen");
 ReadOnlySet set2 = new(set1);

 //set.Add("www.dotnet9.de"); // nicht erlaubt!
 //set.Remove("www.dotnet7.de"); // nicht erlaubt!
 foreach (var url in set1)
 {
  Console.WriteLine(url);
 }

}

New in .NET 8.0 (29): Improvements to the JSON source generatorNew in .NET 8.0 (29): Improvements to the JSON source generator

LEAVE A REPLY

Please enter your comment!
Please enter your name here