A new type of property definitions was already announced for C# 11.0. Now .NET 9.0, with the thirteenth version of the C# compiler, includes the Semi-Auto Properties feature, but it has experimental status. So it is only available if it is in a project file True
Or preview
set.
Advertisement
Dr. Holger Schweitenberg is the technical director of the expert network www.IT-Visions.de, which supports many medium-sized and large companies through consulting and training as well as software development with 53 renowned experts. Through his attendance at numerous national and international conferences as well as more than 90 expert books and more than 1,500 expert articles, Holger Schwittenberg is one of the best-known experts for .NET and web technologies in Germany.
A semi-automatic property (semi-automatic property) closes the gap between full properties and automatic properties. Originally C# only had absolute properties, where you had to implement a getter and a setter for the standard case of reading and writing data, as well as a field as a storage space. Beginning in C# 3.0, automatic properties were introduced, in which you do nothing except read data from the field or save it to the field, if you do nothing else in the getter and setter, the declaration of the field and the getters and The implementation of setters can be eliminated. Since C# 6.0, automatic properties can also be initialized directly during declaration.
However, as getters and/or setters began to include advanced functions such as validation or notification, the automatic property was no longer an option. That’s where the new semi-auto properties come in: you can implement getters and setters, but you don’t have to explicitly declare a field. Instead you use new keyword field
The compiler automatically creates a field for it as storage space.
The following options are available for semi-automatic assets:
- Getters or setters can be omitted; You only need one of the two.
- instead of
set
can also happeninit
used. Then you will have a semi-automatic init-only property. - A semi-automatic property can be initialized with a value at the end.
The development environment indicates in the tooltip that the compiler will create a field in place of the field keyword.
(Image: Screenshot (Holger Schweitenberg))
Introducing a new keyword into a programming language always carries a risk of name collision with existing identifiers. If you have a field with name in your code field
instead you have to @field
Or this.field
To continue using this existing field.
The following code shows a fully automated estate, an automated estate and a semi-automated estate in comparison:
namespace NET9_Console.CS13;
///
class PersonWithID
{
///
public string Name { get; set; } = "unbekannt";
private string company = "Freelancer";
///
public string Company
{
get => company; set
{
if (value == null)
{
throw new ArgumentOutOfRangeException();
}
company = value;
}
}
///
public int ID
{
get;
set // init auch erlaubt!
{
if (value < 0) throw new ArgumentOutOfRangeException();
if (field > 0) throw new ApplicationException("ID schon gesetzt");
field = value;
}
} = -1;
}
///
class CS13_SemiAutoProperties
{
public void Run()
{
CUI.Demo(nameof(CS13_SemiAutoProperties));
PersonWithID p = new PersonWithID();
Console.WriteLine($"{p.ID}: {p.Name} arbeitet bei {p.Company}");
p.ID = 42;
p.Name = "Dr. Holger Schwichtenberg";
p.Company = "www.IT-Visions.de";
Console.WriteLine($"{p.ID}: {p.Name} arbeitet bei {p.Company}");
try
{
p.ID = 43;
}
catch (Exception ex)
{
CUI.PrintError(ex.Message); // "ID schon gesetzt"
}
}
}
(rme)