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

"Constants in C# are best defined as static members" #41677

Closed
kevinfoley opened this issue Jul 9, 2024 · 1 comment · Fixed by #41726
Closed

"Constants in C# are best defined as static members" #41677

kevinfoley opened this issue Jul 9, 2024 · 1 comment · Fixed by #41726
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-csharp/svc help wanted Good for community contributors to help [up-for-grabs] lang-reference/subsvc Pri1 High priority, do before Pri2 and Pri3

Comments

@kevinfoley
Copy link

kevinfoley commented Jul 9, 2024

Type of issue

Other (describe below)

Description

This article includes this Note:

The #define directive cannot be used to declare constant values as is typically done in C and C++. Constants in C# are best defined as static members of a class or struct. If you have several such constants, consider creating a separate "Constants" class to hold them.

Shouldn't this say that constants in C# are best defined with the const keyword? Using static members has different implications for the application, since static members are resolved at runtime and are not necessarily read-only. For primitive values, const in C# is much closer to the behavior of #define in C/C++, since both are resolved at compile time.

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/language-reference/preprocessor-directives.md

Document Version Independent Id

501be68d-5ad0-18d9-8958-54215bb57fc0

Article author

@BillWagner

Metadata

  • ID: 716913e5-dbbe-bdb0-909a-4d8c2dc7be97
  • Service: dotnet-csharp
  • Sub-service: lang-reference
@issues-automation issues-automation bot added dotnet-csharp/svc lang-reference/subsvc Pri1 High priority, do before Pri2 and Pri3 labels Jul 9, 2024
@dotnet-bot dotnet-bot added ⌚ Not Triaged Not triaged labels Jul 9, 2024
@BillWagner BillWagner added the doc-enhancement Improve the current content [org][type][category] label Jul 9, 2024
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label Jul 9, 2024
@BillWagner
Copy link
Member

Good observation @kevinfoley

This section should be rewritten to clarify the distinction between pre-processor constants and const field members.

@BillWagner BillWagner added the help wanted Good for community contributors to help [up-for-grabs] label Jul 9, 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-csharp/svc help wanted Good for community contributors to help [up-for-grabs] lang-reference/subsvc Pri1 High priority, do before Pri2 and Pri3
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants