Pages

C# Features Through the History - Version 9.0

 

The main features of version 9.0!


Version 9.0 of the C# Language came to support the new .NET 5, in the year 2020. Since the release of .NET Core in 2016, this is considered a big breakthrough of the framework, with the unification of the platform:

 


It's natural that the language should go an step further regarding the capabilities, providing interesting features. 

-----------------------------------------------------

How to use the examples 

The examples are available here. You can open the solution and play around. Just uncomment the code and have fun! 




-----------------------------------------------------

Record types

The record type is a new reference type that you can use instead of classes or struct. They provide some interesting capabilities, like the possibility of:
 
- Use of immutable properties with read-only types.
- Value equality, that allows the comparison of all properties and fields values matching.
- Nondestructive mutation: you can create new structures based on the previous one, with modification of properties and fields.
- Inheritance between the records.
 
That means the record types can be a substitute for the DTO's and models, for example.
 

              
        public record Person(string name, int age);

        // inheritance between records
        public record Employee(string register, string name, int age): 
            Person(name, age);

        public static void ExecuteExample()
        {
            var employee = new Employee("ABC", "John", 19);
            var employee2 = new Employee("ABC", "John", 19);
            
            var manager = employee with { age = 65 }; // nondestructive mutation
            // manager.name = "William"; // Error, readonly properties by default

            var person = new Person(age: 1, name: "Marie"); //all mandatory properties

            Console.WriteLine(employee == employee2); // True
            Console.WriteLine(employee == manager);    // False
            Console.WriteLine(ReferenceEquals(employee, employee2)); // False
        }              
              
			  
 
 
Init only setters

With init only setters, it's possible to expande the attribution of some value to a determined property beyond the constructor:
    
                
    // normal way
    internal class Test {
        public DateTime ReleaseDate {get;}
        public int DurationMinutes {get; }

        public Test(DateTime releaseDate, int duration)
        {
            ReleaseDate = releaseDate;
            DurationMinutes = duration;
        }        
    }
    
            var testA = new Test(DateTime.Now, 120);
            
            // error: with the current structure, the atribution of values to the propertis is possible via constructor
            var testB = new Test { 
                ReleaseDate = DateTime.Now,
                Duration = DateTime.Now,
            };    
                
			  
    
                
    // using init setter
    internal class Test2 {
        public DateTime ReleaseDate {get; init;}
        public int DurationMinutes {get; init; }

        public Test2(){}

        public Test2(DateTime releaseDate, int durationMinutes)
        {
            ReleaseDate = releaseDate;
            DurationMinutes = durationMinutes;
        }
    } 
    
            // with the init set property, the possibibility is expanded beyond the constructor
            var test2A = new Test2 {
                ReleaseDate = DateTime.Now,
                DurationMinutes = 60
            };

            test2A.DurationMinutes = 20; // Error: just assigned on initialization    
                
			  
 
 
 
Pattern matching improvements

The pattern matching was introduced in C# 7.0, and it's getting new improvements since then:
    
                  
            var phrase = "3xampl3";

            if (phrase is not null) { // new syntax for null checking
                // pattern-matching improvement
                var firstLetterIsLetter = 
                    phrase[0] is (>= 'a' and <= 'z')
                    or (>= 'A' and <= 'Z');
            }                  
                  
			  
 
 
Half, nint, nuint types

With the expansion of the connected devices, most of them using ARM architecture, the C# language must evolve in the sense of providing a bigger variety of value types, focusing on performance. 
 
The Half is a binary floating-point number that occupies 16 bits. The range is from negative 65,504 to positive 65,504. It's is IEEE 754-compliant (what means it's fully supported by Intel-based PC's, Macs, and most Unix platforms). The idea is save storage space when a fully precision is necessary for some operations:
    
                    
            Half half = Half.MaxValue; // 65500
            var max = nint.MaxValue; // 0x7fffffffffffffff                    
                    
			  
nint and nuint keywords are used to represent a native-sized integers, used mostly for low-level libraries and interop scenarios focused on optimization of performance:
 
    
                      
            nint x = 123;
            nint y = 234;
            nint product = x * y;
            
            IntPtr pt = x;
            pt += 1;
            Console.WriteLine($"x: {x} - pt: {pt}."); // x: 123 - pt: 124.                      
                      
			  
 
 
Target-typed new

Now it's possible to initialize an object in a more straightforward and fun way:
    

    public class TargetTypedExpressions
    {
        StringBuilder sb1 = new();
        StringBuilder sb2 = new ("Test");
        StringBuilder sb;
        
        public TargetTypedExpressions (string initialValue)
        {
            sb = new (initialValue);

            MyMethod (new ("test"));

            Person person = new("James");
        }
        
        void MyMethod (StringBuilder sb) {  }
    }

    internal class Person {
        public Person(string name)
        {
            Console.WriteLine(name);
        }
    }                        
                     
 
 -----------------------------------------

They are many other features that we will incorporate in a near future. I hope you have enjoyed this journey now. Have a nice week!

Here is the entire list of this series:
---------------------------------------------------------------------------------------------------------------------

References:

I'd like to leave with you some references that can be helpful for your development as a software engineer, part of what we talked about above:


Links for books to buy in the USA: 

                 



Links for books to buy in Europe:

                 


Help us to maintain the MyLifeInDev running:


Fabio Ono

No comments:

Post a Comment