Home > Net >  C # 9 init
C # 9 init

Time:05-26

What is the difference between the init accessors and readonly?

CodePudding user response:

I am so understand,
Readonly on statement private fields, the outside is unable to access

Init to use on the properties, the control properties can only be initialized when class instantiation, external is to be able to Get, more strict than private set point;

CodePudding user response:

reference 1st floor Eason0807 response:
I am so understand,
Readonly on statement private fields, the outside is unable to access

Init to use on the properties, the control properties can only be initialized when class instantiation, external is to be able to Get, more strict than private set point;
readonly why must be used in private I don't really understand? I usually is used as a constant

CodePudding user response:

Readonly read-only
Init read-only but in initialization is writeable

For a single class is no different from
In descendant class

CodePudding user response:

refer to the second floor lily's reply:
Quote: refer to 1st floor Eason0807 response:
I am so understand,
Readonly on statement private fields, the outside is unable to access

Init to use on the properties, the control properties can only be initialized when class instantiation, external is to be able to Get, more strict than private set point;
readonly why must be used in private I don't really understand? I usually is used as a constant


Constants on readonly not reinvent the wheel?

CodePudding user response:

Readonly is read-only and cannot be modified in the running
  •  Tags:  
  • C#
  • Related