What is the difference between readonly and constant in c#
Answers were Sorted based on User's Feedback
Answer / karunakar
readonly value can be changed at runtime but constant never
change entire application
| Is This Answer Correct ? | 10 Yes | 0 No |
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 |
What is visual c# net?
What is difference between a constant and read-only in C#?
What are the examples of data types?
What is the use of thread join in c#?
What is the difference between ienumerable and enumerable?
What is dbml file in c#?
What is instantiating a class?
What is difference between array and list in c#?
What is the functionality of break and continue in Visual Studio?
Differentiate between the public and private ?
How are Windows programs different from normal C-programs?
How can I make sure my c# classes will interoperate with other .net languages?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)