const vs readonly Field

<strong><em><span style=”color: #0000ff;”>const 상수(constant)은 컴파일타임(compile-time) 상수이다.</span></em>
<span style=”color: #008000;”>readonly 상수(constant)은 런타임(run-time) 상수이다.</span></strong>
<div>// const
public <strong><span style=”color: #0000ff;”>const</span></strong> int Millennium = 2000;
public <strong><span style=”color: #0000ff;”>const</span></strong> double PI = 3.141592;// readonly
public static <strong><span style=”color: #008000;”>readonly</span></strong> int ThisYear = 2010;</div>
<div></div>
<div>public class MyClass
{</div>
<div style=”padding-left: 30px;”>public <strong><span style=”color: #008000;”>readonly</span></strong> double PI;
public MyClass()
{
PI = 3.141592;
}</div>
<div>}</div>
&nbsp;

<span lang=”EN-US” xml:lang=”EN-US”>const</span> 상수는 선언하는 순간부터 <span lang=”EN-US” xml:lang=”EN-US”>static</span>이 된다<span lang=”EN-US” xml:lang=”EN-US”>.
const 상수를 선언함과 동시에 초기화를 해주어야 한다.
const 상수는 컴파일시 값이 결정 되어져 있어야 한다.</span>

readonly 상수는 static 키워드를 사용하면 static 상수가 된다. 사용하지 않으면 일반상수가 된다.
readonly 상수는 const 키워드를 사용하는 것처럼 반드시 초기화 될 필요없다.
readonly 상수는 생성자를 통해서 런타임시 값이 결정될 수 있다.
한번 값이 결정되면 다시는 그 값을 변경할 수는 없다.