iMasters.com - Knowledge for Developers.
Follow Us
Home Backend What’s New in C# 12 – Using Primary Constructors in Classes and Structs
Backend

What’s New in C# 12 – Using Primary Constructors in Classes and Structs

Capacity was previously restricted to the use of Records with C# 12. We can now also use Primary Constructors in the class implementation and structs. Parameters can be defined in the declaration of a class or struct, making it possible to use them in the body of these structures.

In the next listing, we have an example of this type of implementation with the Projeto class:

-> The Primary Constructor parameters accompany the declaration of the Projeto class name (line 3);

-> The TargetFramework, Descricao, and Tecnologias properties are read-only, receiving the parameters specified via Primary Constructor;

-> A simple constructor can reference the Primary Constructor, as indicated in lines 5 and 6;

-> HorasTrabalhadas follows the behavior of a conventional property, allowing the reading and writing of values.

At the time of this article’s publication, Primary Constructors in classes and structs are still considered a feature in Preview mode. Thus, it becomes necessary to configure the LangVersion attribute in the .csproj file of projects that use this capability with the preview value:

In the next listing, we have examples of using the Primary Constructor of the Projeto type, as well as the conventional constructor that references this element:

When running this application, the result shown in the following image will be produced:

This first project has been made available on GitHub:

https://github.com/renatogroffe/CSharp12-PrimaryConstructorClass

If you find this useful, please give a ⭐️ to support. I also invite you to follow me on GitHub!

The next listing exemplifies the use of a Primary Constructor in a struct called Demografia (line 3), with the values ​​of the different declared parameters being associated with the Pais, Ano, and Populacao properties:

In the following code are examples of using the Demografia struct (including its Primary Constructor):

And when running this application we will have as a result:

This second Console Application has also been made available on GitHub:

https://github.com/renatogroffe/CSharp12-PrimaryConstructorStruct

If you find this useful, please give a ⭐️ to support. I also invite you to follow me on GitHub!

*The content of this article is the author’s responsibility and does not necessarily reflect the opinion of iMasters.

Written by
Renato Groffe

Worked as a consultant in systems development activities for over 15 years. Microsoft MVP (Most Valuable Professional) and MTAC (Multi-Plataform Technical Audience Contributor) program participant. Bachelor in Information Systems, specialization in Software Engineering, and MBA in Business Intelligence. He is also a lecturer and technical author on portals and specialized magazines, focusing on Microsoft technologies and good practices in the software area.

Leave a comment

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Related Articles

Backend

How to Create a Skill for Amazon’s Virtual Assistant Alexa

If you didn’t know, it is not necessary for an Amazon Echo...

Backend

The APIs role in a 5G world

5G is about to revolutionize how we connect and use technology daily....

Backend

EF Core using AsNoTracking with Identity Resolution

Today we will see the consequences of using AsNoTracking with the Identity...

Backend

Understand key features added in ASP.NET Core 7.0

Version 7.0 of the .NET platform brought many new features in this...