C # 9.0 brings concise, unchangeable types

Source: Heise.de added 16th Nov 2020

For all .NET developers who are still working with the classic .NET Framework 4.8 (or even older versions), there is unfortunately a sad one First message: C # 9.0 can no longer be used there. C # 9.0 only runs on .NET 5.0 and .NET Core 3.0 / 3.1 and Xamarin.

Concise types with records Das The absolute highlight in C # are records that can be used to concisely define classes with value semantics. The following line of code, which looks like a constructor preceded by the keyword record , declares a class Person with three properties ID , First name, Name and Status (the latter with default value):

public record person ( int ID, string first name, string name, string status = “unknown”); This record class can now be instantiate like a normal class:

Person hs = new Person (123, “Holger”, “Schwichtenberg”); and use the data contained there via the automatically generated properties:

Console.WriteLine ($ “Person # {hs.ID}: {hs.First name} {hs.Name}”); A difference from a normal one But you notice class immediately when you try to change a property value. This assignment after instantiation

hs.First name = “Dr. Holger”; the compiler no longer allows and acknowledges this with the error

“Error CS 8852 Init – only property or indexer ‘Person.Forname’ can only be assigned in an object initializer, or on ‘this’ or ‘base’ in an instance constructor or an ‘init’ accessor “. The instances of a record class defined in this way are immutable after being instantiated. The new keyword record is a lot of syntactic sugar. With a decompiler like the free ILSpy you can reveal that in reality more than one A4 page of program code was created (see Listing 1).

using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Text; public class Person: IEquatable {protected virtual type EqualityContract { [CompilerGenerated] get {return typeof (Person); }} public int ID {get; init; } public string first name {get; init; } public string Name {get; init; } public string Status {get; init; } public person (int ID, string first name, string name, string status = “unknown”) {this.ID = ID; this.First name = first name; this.Name = name; this.Status = status; base..ctor (); } public override string ToString () {StringBuilder stringBuilder = new StringBuilder (); stringBuilder.Append (“Person”); stringBuilder.Append (“{“); if (PrintMembers (stringBuilder)) {stringBuilder.Append (“”); } stringBuilder.Append (“}”); return stringBuilder.ToString (); } protected virtual bool PrintMembers (StringBuilder builder) {builder.Append (“ID”); builder.Append (“=”); builder.Append (ID.ToString ()); builder.Append (“,”); builder.Append (“First Name”); builder.Append (“=”); builder.Append ((object?) first name); … return true; } [System.Runtime.CompilerServices.NullableContext(2)] public static bool operator! = (Person? r1, Person? r2) {return! (r1 == r2); } [System.Runtime.CompilerServices.NullableContext(2)] public static bool operator == (Person? r1, Person? r2) {return (object) r1 == r2 || (r1?. Equals (r2) ?? false); } public override int GetHashCode () {return (((EqualityComparer . Default.GetHashCode (EqualityContract) – 1521134295 + EqualityComparer . Default.GetHashCode (ID)) – 1521134295 + EqualityComparer . Default.GetHashCode (first name)) – 1521134295 + EqualityComparer .Default.GetHashCode (Name)) – 1521134295 + EqualityComparer . Default.GetHashCode (Status); } public override bool Equals (object? obj) {return Equals (obj as Person); } public virtual bool Equals (Person? other) {return (object) other! = null && EqualityContract == other! .EqualityContract && EqualityComparer .Default.Equals (ID, other! .ID) && EqualityComparer . Default.Equals (first name, other!. First name) && EqualityComparer . Default.Equals (Name, other! .Name) && EqualityComparer . Default.Equals (Status, other! .Status); } public virtual Person $ () {return new Person (this); } protected Person (Person original) {ID = original.ID; First name = original first name; Name = original.Name; Status = original.Status; } public void Deconstruct (out int ID, out string first name, out string name, out string status) {ID = this.ID; First name = this.First name; Name = this.Name; Status = this.Status; }} Listing 1: Decompilation of the record Types “Person” with the ILSpy decompiler

betterCode () presents: .NET 5.0 – The online event on December 3rd 2020 Participants can learn this from experts in the .NET scene:

From .NET Framework via .NET Core to .NET 5.0: What does this mean for the migration, and how big is the effort? What’s new in .NET 5.0? New features : ASP.NET Core 5.0 and get to know Blazor 5.0 The most important language changes in C # 9 Mobile development with .NET 5 OR mapping with Entity Framework Core 5.0 WinUI 3 as an alternative to WPF and UWP Outlook for .NET 6.0

Read the full article at Heise.de

media: Heise.de  
keywords: Console  Mobile  

Related posts


Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 88

Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 88

Related Products



Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 91

Warning: Invalid argument supplied for foreach() in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 91