What is the difference between readonly and constant in c#

Answers were Sorted based on User's Feedback



What is the difference between readonly and constant in c#..

Answer / karunakar

readonly value can be changed at runtime but constant never
change entire application

Is This Answer Correct ?    10 Yes 0 No

What is the difference between readonly and constant in c#..

Answer / anver sadat

A const must be initialized at the time of its creation. A
readonly field can be assigned to once in the class
constructor allowing you to pass in the value at run-time.
Declaring fields as const protects both you and other
programmers from accidentally changing the value of the
field. Also note that with const fields, the compiler
performs some optimization by not declaring any stack space
for the field. The readonly keyword is similar to const,
with two exceptions. First, the storage of a readonly field
is the same as a regular read-write field, and thus there is
no performance benefit. Secondly, readonly fields can be
initialized in the constructor of the containing class.

Use of the 'static' keyword, however, associates a field
with a class itself, so there will only ever be one such
field per class, regardless of the number of the class
instances (and the static field will exist even if there are
no class instances). A static field need not be constant,
however; it can be changed by code.

Is This Answer Correct ?    5 Yes 0 No

Post New Answer

More C Sharp Interview Questions

Can a structure be inherited by a class?

3 Answers  


What are constants in c#?

0 Answers  


What is the best dependency injection c#?

0 Answers  


What are variables in c#?

0 Answers  


This is Kishore i am MCA graduate i have percentage less(52%) in my 10th still i completed .NET course any body tell me how to put fake experience with my BSC degree if you do not mind tell me some fake certificates giving consultencies names in Chennai,Bangalore

5 Answers  


What is a copy constructor in c#?

0 Answers  


what is the default access for a class

0 Answers   Cognizant,


What is the difference between class and abstract class?

9 Answers  


Does constructor return any value in c#?

0 Answers  


What is datatable and dataset in c#?

0 Answers  


Is lazy t thread safe?

0 Answers  


What is serialization in c#?

0 Answers  


Categories