Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

what does readonly means before getter while it also have a setter

I pressed F12 by mistake on Point type and I saw something like:

public int X
{
    readonly get => x;
    set => x = value;
}

I’m not getting this, I know that X is not readonly here, also if it was going to be read-only there shouldn’t be a setter.

what does readonly get mean?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can anybody tell me what the above getter and setter mean?

Update: from the link on Guru’s answer. it says:

Readonly can be applied to some auto-implemented properties, but it won’t have a meaningful effect. The compiler will treat all auto-implemented getters as readonly whether or not the readonly keyword is present.

if it has no effect, why to put it there in the first place.

>Solution :

Since C# 8 you can mark members of struct‘s as readonly to signify that they do not change it’s state. This is used to prevent hidden copies from happening in case of struct used with in modifier:

This normally doesn’t have much impact, except in the case of in parameters. With in parameters for non-readonly structs, the compiler will make a copy of the parameter for each instance member invocation, since it cannot guarantee that the invocation does not modify internal state.

For example as it used in System.Drawing.Point struct.

Read more in Readonly Instance Members specification.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading