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
What is code first? : Entity framework
Which is the default http method for an action method?
What is a model in android?
What is controllercontext?
What is meant by tempdata in mvc?
How does servicing work for the .net framework 3.0?
Do you know about the new features in asp.net mvc 4 (asp.net mvc4)?
What are html helpers in asp.net mvc?
what do you mean by table-per-hierarchy?
Is .net framework going away?
Explain the new features 3.5 framework against with the tool?
What is storage model? : Entity framework
what is entity framework advantage?
Can you explain renderbody and renderpage in mvc?
Why to use Html.Partial in ASP.Net MVC?