What is the Difference between read only and constant
variables?
Answers were Sorted based on User's Feedback
Answer / sandya
Constant fields or local variables must be assigned a value at the time of declaration and after that they cannot be modified. By default constant are static, hence you cannot define a constant type as static.
A const field is a compile-time constant. A constant field or local variable can be initialized with a constant expression which must be fully evaluated at compile time.
public const int X = 10;
A readonly field can be initialized either at the time of declaration or with in the constructor of same class. Therefore, readonly fields can be used for run-time constants.
class MyClass
{
readonly int X = 10; // initialized at the time of declaration
readonly int X1;
}
public MyClass(int x1)
{
X1 = x1; // initialized at run time
}
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / bharathi
Constants:
constant is used for declaring variable as constant and this constant value cannot be modified once after declartration.
It is must to declare the constant value at the time of declaration only.
Example:
constant int y=5;
Readonly
Readonly variables can not be modified like constant,but it is after initialization.
So.it is not mandatory to initialize those variables at declaration time.they can also be initialize after declaration.
Example:
readonly int x;
add()
{
x=5;
}
Both after initilization, we can not modify those values again.
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / isaiselvan
readonly assign a value at run time but we can assign a declaration part.
const values assign only at declaration part.
| Is This Answer Correct ? | 2 Yes | 3 No |
Answer / zia khan
The difference between the readonly and const data members
is that const requires you to initialize with the
declaration, that is directly
| Is This Answer Correct ? | 4 Yes | 6 No |
Can a dictionary have the same key?
What is string pool in c#?
There were a lot of questions asked, so I will list the topic (and add a what is "topic" and know pros/cons). Extreme programming, what is a transaction, various SDLC design approaches, what is a namespace, define a good test case, what is a stored proc, webservice? design patterns? linker? compiler? access modifiers? stack vs. queue? arrays vs. linked lists? sorting algorithms? recursion? OOP principles?
What is anonymous methods in c#?
Do unused Namespaces in c# affect run-time performance?
What is the wildcard character in SQL? Let?s say you want to query database with LIKE for all employees whose name starts with La.
What is difference between Trace and Debug
What is extension method in c# and how to use them?
Is it possible to have a static indexer in c#? Allowed in c#.
What are methods in C#?
What is data bind in c#?
What is difference between const and static in c#?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)