As .dot net 8 is officially released on 14th Nov in.net conference 2023, the latest version of the popular .Net programming language is unveiled. There are some notable features that aims to improve the development experience, code readability and productivity. They are primary constructor, lambda expression with optional parameters, alias any type, collection expression, inline array, ref readonly parameters and experimental attribute. In addition, an experiment feature interceptor is introduced in C# 12 version too. In the upcoming paragraph, I explain those new features in detail and order by used frequency.
Primary constructor
The primary constructor is no longer restricted to record type. You can use the primary constructor in any class and struct. It allows you to write the constructor parameter in the same line as class or struct declaration.
Declaring primary constructor prevent the compiler generating the parameterless constructor to class type. Furthermore, the public properties for primary constructor parameter are only generated in record type, record class and record struct. Neither class type nor structure type is not generated.
The most common used cases are
- serving as argument for base constructor invocation
- initializing member field or property
- to eliminate the boilerplate code in dependency injection
- Referencing the constructor parameter in an instance member.
Optional lambda parameters
In C# 12, you can define optional parameter on lambda expression just like you do in function. Optional parameter allow you to skip passing values as parameter when you call lambda expression. It is very useful when you add parameter to lambda but the parameter is not needed for every existing code. This feature helps you to avoid breaking the existing code during code refactor.
Collection Expression
C# 12 intrduces a collection expression to conise the initializer for collection like Array<T>, List<T> and ReadOnlySpan<T>. In addition, developer can use the new spread (..) operator to combine the elements of one or more collections anywhere.
Alias any Type
Aliasing any type is a convention way to create a semantic aliases for any types, array types, tuple types, pointer types or other unsafe type. You can use using alias directive to do this.
Like other aliases, this alias syntax can be used in the top of the code file or in global using statement.
Including primary constructor, optional lambda parameter, collection expression and alias any type in your program that simplify your code and enhance your code readability. Every new version of C# gives you more fantastic feature helping you write a better code.
Inline array
Inline array allows developer to create an array of fixed size in a struct type. It is a secure way to work with memory buffers, improving app performance because its performance similary to an unsafe fixed size buffer but without involve unsafe code. It reduces the allocation and copying. When your application needs to manipulate a block of memory to store an array of structure. You should use this.
Ref readonly Parameter
The ref readonly parameter provides an approach to passing the parameter read only parameter to method. It uses in a situation that the function needs this variable memory location but don’t need to modify this value. For instance
If you follow the step to create this function, then the compiler issue a warning when you attempt to call this function passing constant or varible without ref keyword. It gives you a warning message expressing your ambiguity implementation.
The purpose of including inline array and ref readonly parameter that is improving the application performance. It makes the app faster.
Experiments attributes
The experiment attributes indicates that those codes are experimental features. This attribute can be used in types, member, and assembly. The program raise error when the code is annotated with experiment attributes but it was invoked by the code without annotated with experiment attribute. In addition, the compiler issues a warning when accessing the code annotated with experiment attribute too. This attribute better to clarify the incomplete feature but want some feedback in the upcoming or preview version.
Interceptors
This feature is still an experimental feature. Therefore, I am not recommend using this feature in production code due to its potential change or removal.
An interceptor is a substitution method. It allows you to redirect a call to another method at compile time.
Experiment attributes and interceptor give you the chance to discover the newest part of C# language. Although interceptor is still an experimental feature and experiment attribute only apply to the code annotated with experimental attribute, but it does help us to understand the code logic and doing code refactor better than before.
Conclusion
I end up the introduction of C#12 new features here. You can read the official documents and its specification for more learning. All in all, C# 12 introduces wealth features to help developers more productivity, efficient and express code in concise. I hope you enjoy this article.
Please leave your email below.