Loading
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
Sachin SinghRank #66 · 26.2K reputationPosted Aug 28, 2022, 9:51 AM EDT
Instead of going for definition , always think in terms of their usage.
1. If something is pure (100%) constant then go for const .
for example distance can be pure constant.
1000m will always be equal to 1KM so, if you have to declare a variable that will hold the conversionParameter then go for const.
These are comiple time constant meaning compiler will throw error if you will assign value to it after declaration, that is why they are initialized at the time of declartion only and then can't be changed.
private const KmToMeter=1000;
// 6*KmToMeter
//7*KmToMeter
2. If something is partially constant, meaning can be changed as per configuration or with chnage in business logic then go for readonly.
Example PI(22/7) the value of PI can be 3.14 or can also be 3.1423 etc depending upon the demand of business , if the accuracy is concern then sometimes you will need up to 2 precision point sometimes upto 4.
so better keep such variable in appsettings or any config file and read from there.
public Class AnyClass
{
private readonly Pi;
public AnyClass()
{
var pi= Configuration.ConfigurationManager.AppSettings["pi"]
}
}
3. If you are dealing with some costly resource that should be shared among all callers (methods of any class) throughout the project, then go for static or better both static and readonly
for example connectionstring , DbContext etc
private static readonly DbContext=null;
public Constructor()
{
DbContext= new DbContext();
}
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Aug 28, 2022, 8:48 AM EDT
Vishal YelveRank #178 · 13.6K reputationPosted Aug 28, 2022, 8:00 AM EDT
Please refer below article for difference between readonly vs constant.
https://www.c-sharpcorner.com/blogs/difference-between-constant-and-readonly
Shiv Prasad Koirala sir Video.
https://youtu.be/vNPBWNNlDwM
OR
Refer below url for static, readonly and constant
https://youtu.be/IYRNf1OFhsM
Anand SA99 reputationPosted Aug 31, 2022, 11:12 AM EDT
Uday DodiyaRank #239 · 10.3K reputationPosted Aug 28, 2022, 7:57 AM EDT
Here 5 Major Difference Are Following
1.
static- Declared using the static keyword.
readonly - Declared using the readonly keyword.
const - Declred using the const keyword. By default a const is static that cannot be changed.
2.
static - Classes, constructors, methods, variables, properties, event and operators can be static. The struct, indexers, enum, destructors, or finalizers cannot be static.
readonly - Only the class level fields can be readonly. The local variables of methods cannot be readonly.
const - Only the class level fields or variables can be constant.
3.
static - Static members can only be accessed within the static methods. The non-static methods cannot access static members.
readonly - Readonly fields can be initialized at declaration or in the constructor.Therefore, readonly variables are used for the run-time constants.
const - The constant fields must be initialized at the time of declaration.Therefore, const variables are used for compile-time constants.
4.
static - Value of the static members can be modified using ClassName.StaticMemberName.
readonly - Readonly variable cannot be modified at run-time. It can only be initialized or changed in the constructor.
const - Constant variables cannot be modified after declaration.
5.
static - Static members can be accessed using ClassName.StaticMemberName, but cannot be accessed using object.
readonly - Readonly members can be accessed using object, but not ClassName.ReadOnlyVariableName.
const - Const members can be accessed using ClassName.ConstVariableName, but cannot be accessed using object.
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Nov 11, 2022, 3:25 AM EST
https://www.tutorialsteacher.com/articles/difference-between-static-readonly-constant-in-csharp