Skip to content
New issue

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

CA1802 documentation needs to warn about the downsides #40827

Open
kevincathcart-cas opened this issue May 9, 2024 · 0 comments
Open

CA1802 documentation needs to warn about the downsides #40827

kevincathcart-cas opened this issue May 9, 2024 · 0 comments
Assignees
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-fundamentals/svc help wanted Good for community contributors to help [up-for-grabs] Pri2

Comments

@kevincathcart-cas
Copy link

Type of issue

Missing information

Description

This rule suggests changing "static readonly" fields whose value is set to a compile time constant to "const" when possible.

For public members this is potentially risky if there is any chance these values might change in the future. This is because of how const values get copied into dependent assemblies when used, which does not happen with static readonly fields. This means that if the value might change in a later version of a library, and if when it changes, other libraries compiled against the older version might misbehave as a result of still using the old value, then this warning should be suppressed instead.

The documentation should really at least mention this concern. Of course, this only helps people who read the documentation, but for those who do, at least they become fully informed of the risks involved in following this analyzer's advice.

I will clarify that this analyzer's suggestion is always fine for members with net private accessibility, and is fine for members with net internal accessibility if no InternalsVisibleTo is present (or if those dlls are always built in the same solution, and bundled into the same package or is not packaged and both are deployed together).

That safety is because a whole assembly is compiled at once so the used values will inherently be consistent even if it changes in the future, (except for the super uncommon, and generally ignored edge case of multi-module assemblies).

Page URL

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1802

Content source URL

https://github.com/dotnet/docs/blob/main/docs/fundamentals/code-analysis/quality-rules/ca1802.md

Document Version Independent Id

6b33a042-4440-65b8-2d4c-ab980124eec0

Article author

@gewarren

Metadata

  • ID: b2a769de-cd65-9b02-872e-30186186690d
  • Service: dotnet-fundamentals
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label May 9, 2024
@gewarren gewarren added the doc-enhancement Improve the current content [org][type][category] label May 10, 2024
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label May 10, 2024
@gewarren gewarren added help wanted Good for community contributors to help [up-for-grabs] ⌚ Not Triaged Not triaged labels May 10, 2024
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label May 10, 2024
@gewarren gewarren self-assigned this May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-fundamentals/svc help wanted Good for community contributors to help [up-for-grabs] Pri2
Projects
None yet
Development

No branches or pull requests

3 participants