Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set read-only property in C # 9.0

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In view of how to set the read-only property in C# 9.0, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

In order to enrich C # support for functional programming, newer versions of C # have introduced some useful new features. For example, readonly modifier support is added to struct-type methods in C # 8, and methods modified by readonly cannot modify the properties of the class in which the method belongs. For example:

Public struct FooValue

{

Private int A {get; set;}

Public readonly int IncreaseA ()

{

A = A + 1; / / error report

Return A

}

}

On the other hand, C# 9 has further added read-only support, this time adding the init-only attribute and record-related features, as described below.

Init-only attribute

We know that the properties of the class have set and get accessors, and now C# 9 adds a property accessor: init. Init is a variant of the set accessor. Its purpose is to assign a property to an object only when it is initialized, and then the property is read-only, so it is called the init-only property. The mode of use is as follows:

Public class Foo

{

Public string PropA {get; init;}

Public string PropB {get; init;}

}

Assignment operation:

Var foo = new Foo {PropA = "A", PropB = "B"}

Foo.PropA = "AA"; / / error report, PropA is read-only at this time!

Because init is assigned during the initialization phase, it can modify readonly-decorated fields within the class. For example:

Public class Foo

{

Private readonly string propA

Private readonly string propB

Public string PropA

{

Get = > propA

Init = > propA = (value? Throw new ArgumentNullException (nameof (propA)

}

Public string PropA

{

Get = > propB

Init = > propB = (value? Throw new ArgumentNullException (nameof (propB)

}

}

You will understand this if you know that you can assign values to read-only fields / properties in the constructor.

Record (Record)

Anyone who has worked in the financial system knows that once a transaction is entered, it cannot be changed. If it is wrong, it is necessary to enter a new negative record to wash out the previous red, and then enter the correct record. In response to scenarios such as read-only records, C# 9 introduces the concept of Record (record, which is used later in Chinese for "record"), which is used to support the read-only nature of the entire object (that is, read-only when instantiated). The mode of use is as follows:

Public data class Foo

{

Public string PropA {get; init;}

Public string PropB {get; init;}

}

A data keyword is used here to indicate that the object of this class is just a pure record value, not a modifiable state (in functional programming, all data modifications are state changes).

The above is so troublesome that it can be abbreviated like this:

Public data class Foo

{

String PropA

String PropB

}

The default property is public, and if you really want to change it to private, you can precede the property definition with the private modifier.

Location record (Positional Record)

Sometimes, to make initialization more convenient, you can define a constructor to assign a value to an attribute. You only need to pass the attribute values to the constructor sequentially during initialization, which is called Positional Construction. Similarly, the deconstruction function (Deconstructor) can also be used to realize the deconstruction of attributes, that is, the values of attributes are extracted from the object according to the parameter order of the deconstruction function, which is called location deconstruction (Positional Deconstructor). Records that realize location construction or location deconstruction are called location records (Positional Record). The following is an implementation of a location record:

Public data class Foo

{

String PropA

String PropB

Public Foo (string propA, string propB)

= > (PropA, PropB) = (propA, propB)

Public void Deconstruct (out string propA, out string propB)

= > (propA, propB) = (PropA, PropB)

}

This is too troublesome to write. It can be simply abbreviated as:

Public data class Foo (string PropA, string PropB)

In such a short sentence of code, the init-only automatic properties are implemented by default, and constructors and deconstructors are defined for all properties at the same time.

Examples of use:

Var foo = new Foo ("AA", "BB"); / / structure location

Var (a, b) = foo; / / deconstruction location

As you can imagine, most of the recorded usage scenarios, the above abbreviations can meet the needs. If there is a special scene, it can not be simple, you need to customize and modify its default behavior.

With expression

When dealing with immutable data, to generate different states, a common scenario is to copy a new record based on an old record. For example, if we want to modify the PropA property of the Foo object, we need to copy the object to generate a new object. This operation is called "non-destructive modification (non-destructive mutation)" in functional programming. To support this operation of records, C # 9 introduces with expressions, which make it easy to create a new record based on an existing record. Example:

Var other = foo with {PropA = "AA"}

The with expression is actually implemented internally through a default protected constructor, roughly as follows:

Protected Foo (Foo original)

{

/ / copy all fields of original

} this is the answer to the question about how to set the read-only property in C# 9.0. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report