2

Possible Duplicate:
What is the difference between const and readonly?

i have doubt in c# what is difference in constant and readonly explain with simple problem or any reference.

Community
  • 1
  • 1
Surya sasidhar
  • 29,607
  • 57
  • 139
  • 219

8 Answers8

12

A const is initialized at compile time, and cannot be changed.

A readonly is initialized at runtime, but can be done only one time (in the constructor, or inline declaration).

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
chriszero
  • 1,311
  • 3
  • 13
  • 26
4

const is a compile-time constant: the value you give is interpolated into your code as it's compiled. This means that you can only have constant values of types that the compiler understands, for example integers and strings. Unlike readonly, you couldn't assign the result of a function to a const field, for example:

class Foo {
  int makeSix() { return 2 * 3; }
  const int const_six = makeSix(); // <--- does not compile: not initialised to a constant
  readonly int readonly_six = makeSix(); // compiles fine
}

A readonly field, on the other hand, is initialised at runtime, and can have any type and value. The readonly tag simply means that the field can never be re-assigned once it's been set.

Beware, though: a readonly collection or object field can still have its contents changed, just not the identity of the collection that's set. This is perfectly valid:

class Foo {
  readonly List<string> canStillAddStrings = new List<string>();

  void AddString(string toAdd) {
    canStillAddStrings.Add(toAdd);
  }
}

but we would not be able to replace the reference in this case:

class Foo {
  readonly List<string> canStillAddStrings = new List<string>();

  void SetStrings(List<string> newList) {
    canStillAddStrings = newList; // <--- does not compile, assignment to readonly field
  }
}
Andy Mortimer
  • 3,619
  • 20
  • 14
3

Constants (const) must be known at compile time.

readonly fields can be initialized (and only initialized) with a value known at run time.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
2

A const can never change, the declared value is burnt into the code at compile-time.

A readonly field is initialised at runtime, but can only be initialised either in the object's constructor or in the field declaration itself.

LukeH
  • 263,068
  • 57
  • 365
  • 409
2

Since const values are evaluated at compilation time if you use a const in assembly1 defined in assembly2 you would need to compile again assembly1 in case that the value of the const changes. This doesn't happen with readonly fields since the evaluarion is on runtime.

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • +1 for mentioning the recompilation "issue". – Benjamin Podszun Apr 01 '10 at 11:21
  • 2
    The recompilation issue should *never* be an issue with well-designed code because the value of a `const` should *never* change. If the value of a `const` does change then it wasn't really a constant in the first place, and should not have been declared as such. – LukeH Apr 01 '10 at 15:01
1

constast are initialized at runtime and can be used as static members. readonly can be intialized at runtime and only once.

KhanS
  • 1,167
  • 2
  • 12
  • 27
0

In c#, constant members must be assigned values at compile time. But you can initialize once readonly members in run time.

fkucuk
  • 631
  • 1
  • 9
  • 21