본문 바로가기

IT_플밍

[C#] readonly 와 const의 차이점.

readonly(C# 참조)

위에 링크는 Microsoft 공식 문서글이다. 읽고 정리하고자 적는다.


 

 Static 여부

 초기화

코드 

readonly 

 기본 Static

 필드 선언 시에서만


1
private const int test = 100;

cs

const

 앞에 Static 을 써줘야함

 필드 선언 or 생성자

1
2
3
4
5
6
7
8
9
10
11
private readonly int test;
 
public TestClass()
{
    test = 10;
}
 
public TestClass(int value)
{
    test = value;
}

cs


readonly 은 확장성을 가진 const 라고 보면 된다.


하지만 switch case 문에서 readonly string 을 허용하지 않는다고 한다. (2016.10.31일자 기준)