Software Engineering Asked on October 29, 2021
What’s the point of ever using const
?
It is more restrictive than static readonly
:
const
in a dependency won’t be reflected in its consumersconst
for certain primitive typeswith both these restrictions in mind, perhaps among others I am not aware of, why ever use const
?
Why not always use static readonly
to maintain consistency of style?
Things declared const
are true compile-time constants, and thus may occur in constant-expressions. This means you can use them in the following contexts, where static readonly
variables may not appear[1]:
const int OTHER = N;
enum Foo { Bar = N }
void Fn(int i = N)
case
labels: case N:
and goto case N;
[Value(N)]
new int[N] { 1, 2, 3 }
Answered by Sebastian Redl on October 29, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP