I want this approach.
const public int x;
at runtime
x = 10; //this value will change it another Class --> (Not internal)
x--> never change
is that possible any ways?
You cant assign value to const variable at runtime but still you can achieve your requirement logically,
You can create static readonly property, and a static constructor and assign value from the static constructor
public class ClassName
{
static readonly int x;
static ClassName()
{
x = 10;
}
}
the compiler act as same on const property and static property, memory allocation is also same
All constants declarations are implicitly static
ref https://blogs.msdn.microsoft.com/csharpfaq/2004/03/12/why-cant-i-use-static-and-const-together/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With