What is the difference between static, readonly, and constant in C#
Loading
What is the difference between static, readonly, and constant in C#
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.
Sachin SinghPosted Aug 28, 2022, 1:51 PM
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();
}
Brahma Prakash ShuklaPosted Nov 11, 2022, 8:25 AM
https://www.tutorialsteacher.com/articles/difference-between-static-readonly-constant-in-csharp
Anand SAPosted Aug 31, 2022, 3:12 PM
Rajanikant HawaldarPosted Aug 28, 2022, 12:48 PM
Vishal YelvePosted Aug 28, 2022, 12:00 PM
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
Uday DodiyaPosted Aug 28, 2022, 11:57 AM
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.