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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jaish MathewsRank #225 · 11K reputationPosted Nov 11, 2024, 3:13 AM EST
Please find below a tabular representation that I have sourced from other references
Charles Henington54 reputationPosted Nov 10, 2024, 12:35 AM EST
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.
Benjamin Gabriel10 reputationPosted Nov 10, 2024, 2:08 AM EST
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.Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 11, 2024, 1:07 AM EST
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:
public class Example { public static int StaticCounter = 0; }public class Example { public readonly int ReadOnlyValue; public Example(int value) { ReadOnlyValue = value; } }constkeyword and must be initialized at the time of declaration:public class Example { public const int ConstantValue = 100; }Implementation Scenarios
Vishal YelveRank #178 · 13.6K reputationPosted Nov 12, 2024, 8:08 AM EST
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