What is the difference between const and readonly in c#.net?



What is the difference between const and readonly in c#.net?..

Answer / Leena Rani

In C#, both const and read-only variables are used to hold constant values, but there are some differences:
- const variables must be initialized at the time of declaration and their value cannot change. They are compiled into the assembly itself.
- readonly fields can be initialized either at the time of declaration or in the constructor. They are stored as fields within an object and can have different values for each instance.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Sharp Interview Questions

What is a clr host?

1 Answers  


Why do I get an error (cs1006) when trying to declare a method without specifying a return type?

1 Answers  


Is goto statement supported in C#? How about Java?

2 Answers  


How many constructors can a class have c#?

1 Answers  


What are get and set in c#?

1 Answers  


how to cleanup object that does not support dispose method? How to implement dispose for such scenarios?

2 Answers   ITC Infotech,


How can you overload a method?

3 Answers  


Are arrays immutable c#?

1 Answers  


Illustrate the differences between the system.array.copyto() and system.array.clone()?

1 Answers  


Why do we use class in c#?

1 Answers  


Define encapsulation?

1 Answers   Siebel,


Can datetime be null c#?

1 Answers  


Categories