C#에서 Java의 final 키워드와 같이 상수를 정의하는 키워드는 readonly, const 두가지가 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
public class SamleClass
{
private readonly string strConstant1;
// const 한정자 : 초기 값을 지정하지 않으면 컴파일 오류 발생
private const string strConstant2 = "const";
public SampleClass()
{
// readonly 한정자 : 클래스 생성자에서 값 할당 가능
strConstant1 = "readOnly";
}
} |
cs |