Skip to main content

Posts

Showing posts with the label C# 12.0 features

These 5 new features in C# 12 - Change your way of coding

C# 12 introduces several exciting new features aimed at improving developer productivity, enhancing language expressiveness, and providing better performance. Below are five new features in C# 12 that stand out: 1. Primary Constructors for Classes C# 12 brings primary constructors for classes, a feature previously available only for structs. This simplifies the way you declare constructors and initialize class properties. public class Person ( string name, int age ) { public string Name { get ; } = name; public int Age { get ; } = age; } var person = new Person( "John Doe" , 30 ); Console.WriteLine( $" {person.Name} , {person.Age} " ); With primary constructors, the parameters are passed directly when creating an instance of the class. This eliminates the need for boilerplate constructor code and reduces redundancy. Never miss new posts by subscribe Subscribe Powered by 2. Collection Expressions Collection expressions make it eas...

C# : 12.0 : Collection expressions

Introduction Collection expressions is a new feature introduced in C# 12.0. Lot of us know about collections and very familiar with generic collections also. We know that in collections, lot of flavours like arrays, lists.. etc. Till C# 11.0 different collections used different syntaxes for initialization. Let us go through the old syntaxes which will be revision for us. In this article, will demo both arrays and list. Arrays are one of the collection , we rapidly use in many scenarios. Will have a look on different ways of array initializations // old way of initialization int[] array1 = new int[] { 1, 2, 3, }; int[] array2 = new [] {1, 2, 3, }; int[] array3 = { 1, 2, 3, }; In above code, initialized arrays in different ways till C# 11.0. Even in C# 12.0 also we can follow same way of initialization. But in new version, introduced new way of initialization // new way in collection expressions int[] array4 = [1, 2, 3,4]; In the same way will h...

C# : 12.0 : Primary constructor

Introduction In C# 12.0, the introduction of the "Primary Constructor" simplifies the constructor declaration process. Before delving into this concept, let's revisit constructors. A constructor is a special method in a class with the same name as the class itself. It's possible to have multiple constructors through a technique called constructor overloading.  By default, if no constructors are explicitly defined, the C# compiler generates a default constructor for each class. Now, in C# 12.0, the term "Primary Constructor" refers to a more streamlined way of declaring constructors. This feature enhances the clarity and conciseness of constructor declarations in C# code. Lets see an simple example code, which will be known to everyone. public class Version { private int _value ; private string _name ; public Version ( int value , string name ) { _name = name ; _value = value ; } public string Ve...