Hi
Any one can explain the difference between
Static
Read-only
Constant
Where we need to implement this feature
Hi
Any one can explain the difference between
Static
Read-only
Constant
Where we need to implement this feature
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaish MathewsPosted Nov 11, 2024, 8:13 AM
Please find below a tabular representation that I have sourced from other references
Vishal YelvePosted Nov 12, 2024, 1:08 PM
Hi Kiran,
do refer below links
Constant vs Read-only
Constants are compile-time constants.
ReadOnly is Runtime constant.
https://youtu.be/vNPBWNNlDwM?si=GbFnq-XK2cwHgIGU
Static
https://www.c-sharpcorner.com/UploadFile/36bc15/static-keyword-in-C-Sharp/
https://youtu.be/N9xwjZ4mMvw?si=pRnckwy7LTia1JUa
Jayraj ChhayaPosted Nov 11, 2024, 6:07 AM
Hi @kiran kumar,
In C#,
static,read-only, andconstantare keywords that define the behavior of variables, but they serve different purposes.Static: A static variable belongs to the class itself rather than any instance of the class. It is shared across all instances. For example:
constkeyword and must be initialized at the time of declaration:Implementation Scenarios
Benjamin GabrielPosted Nov 10, 2024, 7:08 AM
Static: Static means a variable or method belongs to the class itself, not to any specific object created from that class. This means there’s only one copy of a static variable or method shared by all objects. It’s helpful for things that should be the same across all instances, like a counter to track how many objects have been created, or utility functions that don’t need any specific object data.
Read-only: A read-only variable can only be assigned once, either when it’s declared or in the constructor. It’s useful when you want a variable that’s specific to each object but shouldn’t change after it’s set, like an ID number that stays the same after the object is created.
Constant: A constant is a value that is set once and fixed for the entire application. It’s defined at compile time and cannot be changed. This is useful for values that are always the same, like
PIor application settings that don’t vary.Charles HeningtonPosted Nov 10, 2024, 5:35 AM
static vs const.... a static value can be changed and a const value will always have the value assigned. readonly can be assigned in a variable initalizer or in a constructor.
It all depends on your code needs which you should use.
An example, System.Drawing.Color uses static readonly for their Colors.
public static readonly Color Black;
static Color()
{
Black = Color.FromArgb(255, 0, 0, 0);
}
when accessed outside of Color class it will always have the same value for Black and can not be changed.
if it were static Color Black;
then outside of Color class we could say Color.Black = Color.White then Black and White would be the same.
obviously this would be undesirable.