Click here to Skip to main content
15,899,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

Could you tell me that, How the Readonly is differ from Constant in C#.

Thank you,
Posted

Contant must be initialized at declaration time, readonly can be initialized on the constructor (and thus have a different value depending on the constructor used). A const is a compile-time constant whereas readonly allows a value to be calculated at run-time and set in the constructor or field initializer.

The difference is that the value of a static readonly field is set at run time, and can thus be modified by the containing class, whereas the value of a const field is set to a compile time constant.

In the static readonly case, the containing class is allowed to modify it only:
1. in the variable declaration (through a variable initializer)
2. in the static constructor (instance constructors, if it's not static)

static readonly is typically used if the type of the field is not allowed in a const declaration, or when the value is not known at compile time.

Instance readonly fields are also allowed.


--Amy
 
Share this answer
 
v2
Readonly: Readonly means once you assigned a value you can not change that value and more over it has flexibility to assign value once after declaration . It maintains same no. of copies as objects created of the class.

Constant: Constant means once you declared a variable as constant you have to assign value i.e you need to assign value during declaration. It maintains a single copy in the life cycle of a class.

For more See This[^] link
 
Share this answer
 
Two differences:
1) A constant value is a fixed item which is known absolutely at compile time, and has an implicit static declaration: the vlaue is the same for each any every instance of the class, and it can be accessed via the class name without an instance being required.
2) A readonly value is not fixed at compile time, but it can only be assigned in the class constructor. This allows for parameter values to be assigned to a readonly variable, but never be changed again. readonly values do not have to be static and can be instance based, and thus different for each instance of a class.
 
Share this answer
 
Comments
Manikandan MAC 1-Aug-14 7:08am    
Thank you, I understood, Could you tell me that, when we can use readonly and constant.
OriginalGriff 1-Aug-14 7:27am    
You use const values to replace numbers in your code with values that will never change:
private const int WM_NCPAINT = 0x0085; // Windows message, Non-Client Paint
private const string tableName = "Customers";

You use readonly when the value can be different for each class instance (or read from an initialization file perhaps) but which can't be changed once it is set. An ID value which ties a class to a row in a database perhaps, or a connection string you read from a file.
Manikandan MAC 1-Aug-14 7:35am    
I understood, Thank you very much,
OriginalGriff 1-Aug-14 7:39am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900