in Technology by
What is Init only setters C#.Net?

1 Answer

0 votes
by

Init only setters provide consistent syntax to initialize members of an object. Property initializers make it clear which value is setting which property. The downside is that those properties must be settable. Starting with C# 9.0, you can create init accessors instead of set accessors for properties and indexers. Callers can use property initializer syntax to set these values in creation expressions, but those properties are readonly once construction has completed. Init only setters provide a window to change state. That window closes when the construction phase ends. The construction phase effectively ends after all initialization, including property initializers and with-expressions have completed.

You can declare init only setters in any type you write. For example, the following struct defines a weather observation structure:

public struct WeatherObservation { public DateTime RecordedAt { get; init; } public decimal TemperatureInCelsius { get; init; } public decimal PressureInMillibars { get; init; } public override string ToString() => $"At {RecordedAt:h:mm tt} on {RecordedAt:M/d/yyyy}: " + $"Temp = {TemperatureInCelsius}, with {PressureInMillibars} pressure"; }

Callers can use property initializer syntax to set the values, while still preserving the immutability:

var now = new WeatherObservation { RecordedAt = DateTime.Now, TemperatureInCelsius = 20, PressureInMillibars = 998.0m };

But, changing an observation after initialization is an error by assigning to an init-only property outside of initialization:

now.TemperatureInCelsius = 18;

Init only setters can be useful to set base class properties from derived classes. They can also set derived properties through helpers in a base class. Positional records declare properties using init only setters. Those setters are used in with-expressions. You can declare init only setters for any class or struct you define.

Related questions

0 votes
    I have a class which has 2 sets of getter & setters. 1 set is the traditional type. These work ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    Here is a pure Python-specific design question: class MyClass(object): ... def get_my_attr(self): ... def ... you use and why? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    What’s the init container and when it can be used in Kubernetes?...
asked Jul 15, 2021 in Technology by JackTerrance
0 votes
    Which of the following is true about init() method of servlet? A - The init() method simply creates or loads some data that will ... C - Both of the above. D - None of the above....
asked Jan 12, 2021 in Technology by JackTerrance
0 votes
    When init() method of servlet gets called? A - The init() method is called when the servlet is first created. B - The init() ... . C - Both of the above. D - None of the above....
asked Jan 12, 2021 in Technology by JackTerrance
0 votes
0 votes
    When I am running a Hadoop .jar file from the command prompt, it throws an exception saying no such method ... (Child.java:264) Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    Q)Which is not life cycle method of Servlet ? * service ( ) delete ( ) init ( ) doPost ( ) Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    Q)Which is not life cycle method of Servlet ? * service ( ) delete ( ) init ( ) doPost ( ) Select the correct answer from above options...
asked Dec 18, 2021 in Education by JackTerrance
0 votes
    In Azure, I have a small app deployed: https://jsnamespace.azurewebsites.net/. In localhost, it works ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    In Azure, I have a small app deployed: https://jsnamespace.azurewebsites.net/. In localhost, it works ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    What is Garbage Collection in C#.Net?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is Reflection in C#.Net?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is File Handling in C#.Net?...
asked Apr 1, 2021 in Technology by JackTerrance
0 votes
    What is Fit and finish features in C#.Net?...
asked Dec 30, 2020 in Technology by JackTerrance
...