What is difference between constants, readonly and, static ?
What is difference between constants, readonly and, static ?
Hi friends
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
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
Deepika Chaudhary133 reputationPosted Nov 30, 2012, 10:08 PM EST
Constant
A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the
constkeyword and must be initialized as they are declared. For example;public class MyClass { public const double PI = 3.14159; } Changing the value of const variable will give compile time error. ReadOnly A read only member is like a constant as it represents an unchanging value. The difference is that a readonly member can be initialized at runtime. Which cannot be do in const variable. For e.g. public class MyClass { public readonly double PI; public MyClass() { PI = 3.14159; } } Static This means that the member can be accessed without creating an instance of the class. o access a static class member, use the name of the class instead of a variable name to specify the location of the member. For example: int i = Car.NumberOfWheels;