What is the difference b/w constant & read only variable?
Answer Posted / sumesh
For Both Constants and Read only values, we can set values
for them only once.
In the case of Constants, we set the value at the compile
time itself. After that we cannot change it
Example:
class Test
{
const double pi = 3.14;
.......................
.......................
}
In the case of Read only data, we can set the value in the
constructor.
Example:
class Test
{
private double _bonus;
public Test(double bonus)
{
_bonus = bonus;
}
public double Bonus
{
get { return _bonus; }
}
}
Test a(5.25);
Test b(6.75);
In this case, at the time object creation, we can set the
value for the bonus. That is, we can decide the value at
the runtime. So a reaonly variable is also known as runtime
constants.
So, the basic difference is, in the case of contstant, the
value will be same across the objects of that class, but it
may differ in the case of readonly variables.
| Is This Answer Correct ? | 16 Yes | 0 No |
Post New Answer View All Answers
Is .net framework 4.8 the last version?
What is the advantage of mvc?
i want to take the fast track training on >net Frame work and work flows and share point could you tellme the good institutes for that cources
What is connected scenario? : Entity framework
How to make sure Client Validation is enabled in ASP.Net MVC
What is the use of viewmodel in mvc?
What are Action Filters in ASP.NET MVC and its use?
What is Dependency Injection in ASP.Net MVC
Can we free memory explicitly without waiting for garbage collector to free the memory in .net compact framework?
How to change the action name in mvc?
What are the versions of .net framework?
What is iobjectset? : Entity framework
How route table has been created in ASP.NET ASP.Net MVC?
Is .net framework backwards compatible?
What is the use of view model in asp.net mvc?