C# const, readonly
const, readonly
const, readonly 🌈
const :
- 한번 값이 할당되면 그 이후 변경이 불가능하다.
- static 변수이다.
- 클래스, 구조체 및 배열을 비롯한 사용자 정의 형식의 타입을 사용할 수 없다. (only primitive types, enums and string)
- 컴파일 타임에 처리된다.
- 컴파일 시 할당한 값이 어셈블리 메타데이터에 기록이 된다. (컴파일 상수)
- 배포 후 업데이트가 일어나면 참조하고 있는 어셈블리는 모두 리빌드를 해주어야한다.
readonly :
- 선언과 동시에 초기화를 하지 않아도 된다.
- 생성자에서 한번 더 할당이 가능하다.
- static 키워드와 사용되면 static 생성자에서만 할당이 가능하다.
- 모든 타입이 가능하다.
- 런타임에 처리된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
private readonly Person person;// = new Person("홍길동", 24);
private static readonly Person person2 = new Person("심봉사", 24);
private const float Pi = 3.14f;
// constructor
public Program()
{
person = new Person("홍길동", 24);
// Pi = 3.1415f; (X)
// person2 = null; ( X )
}
static Program()
{
person2 = null;
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
This post is licensed under CC BY 4.0 by the author.