We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow primary class constructor parameters to only be used to initialize fields/properties, and not inside methods.
Since:
this
Okay:
public sealed class Example(string example) { private readonly string example = example; public void Method() { Console.WriteLine(this.example); } }
Not okay:
public sealed class Example(string example) { public void Method() { Console.WriteLine(example); } }
The text was updated successfully, but these errors were encountered:
They cannot be made read-only.
The backing field is read-only.
https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQAUEEsC2UECeAwgPYB2yMCcAxjCQsgDQAmIA1AD4ACAzAATJoAGwgt+3AEz8AogA8oOAA6iAFNwCMABn4QFy0QEoAsACgA3mf7X+S7ADdYEfkigtywghO279K5wC8vor+ANxmAL5AA==
Or did you mean something else?
Sorry, something went wrong.
That's the "okay" code, when you assign the parameter to a read-only field.
This is the "not okay" code:
https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQAUEEsC2UECeAwgPYB2yMCcAxjCQsgDQAmIA1AD4ACATAIwBYAFAjuAZgAEyaABsILSX0kBRAB5QcAB3kAKbvwAMkiBu3yAlCIDeIyfaVTuAFkkBZCDAAWJFrqvCDpK2gUEOBgCcuqaaOhAWANx2DgC+IilAA===
... where the parameter (example) is mutable, and available anywhere within the class without qualification.
example
I'd actually like to be able to use primary constructor arguments in my whole class and would favor a rule telling me to handle them as read-only.
No branches or pull requests
Allow primary class constructor parameters to only be used to initialize fields/properties, and not inside methods.
Since:
this
qualification everywhere within the class body.Okay:
Not okay:
The text was updated successfully, but these errors were encountered: