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


Please Help Members By Posting Answers For Below Questions

what do you mean by navigation property?

594


Can you explain renderbody and renderpage in asp.net mvc?

555


Can you Write the GC(Garbage Collector) Algoritham in .NET? (But not Explination of Working of GC).

1800


What is the use of viewmodel in mvc?

614


Which is the default http method for an action method?

507






Can I add asp.net mvc testcases in visual studio express?

595


Can you set the unlimited length for "maxjsonlength" property in config?

561


Asp.net mvc application, makes use of settings at 2 places for routing to work correctly. What are these 2 places?

628


Explain peek method in tempdata in asp.net mvc?

585


How to return the JSON from action method in ASP.Net MVC?

692


How large is the .net framework 3.0? Does this change make the release larger?

552


What are actions in asp.net mvc?

573


What is viewbag?

614


What is the difference between model and entity?

493


What type of filter does outputcacheattribute class represents?

671