usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;publicclassConstSample{publicconstdouble 로켓공식 =3.14;// 절대로 바뀌면 안되는 정보publicvoid 함수(){//로켓공식 = 4.2213; // 정수가 아니라 상수라서 값을 바꿀 수 없다}}publicclassConstTest{publicstaticvoidMain(string[] args){
Console.WriteLine(ConstSample.로켓공식);// 객체 선언해서 메모리 할당X -> static이기 때문에 프로그램이 시작하자마자 바로 메모리가 할당되므로// 다이렉트로 접근 가능!}}
readonly 상수
const 와의 차이점 (readonly 상수의 특징)
반드시 초기화할 필요없다.
생성자에서 딱 한번 값을 할당할 수 있다.
const와는 다르게 자동으로 static이 되지 않는다.
static 키워드를 사용하면 스태틱 상수가 된다.
static 키워드를 사용하지 않으면 일반 상수가 된다.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;publicclass 로켓외부요인
{// readonly 스태틱 방식// readonly 일반 방식publicreadonlystaticint 날씨맑음_STATIC_READONLY;// readonly로 선언한 상수 : 초기화를 안시켜도 에러가 안남publicreadonlyint 날씨흐림_NORMAL_READONLY;// 1. static 생성자static 로켓외부요인(){// readonly에서는 static은 static끼리 할당 가능함
날씨맑음_STATIC_READONLY =100;//날씨흐림_NORMAL_READONLY = 200; // static이 아니라서 할당 불가능}// 2. 일반 생성자
로켓외부요인(int 바람세기){//날씨맑음_STATIC_READONLY = 100; // static이라서 할당 불가능
날씨흐림_NORMAL_READONLY = 바람세기;}publicstaticvoidMain(string[] args){// 1. static readonly 호출 : static 호출방식 (별도의 메모리 할당과정 없이 바로 사용)
Console.WriteLine(로켓외부요인.날씨맑음_STATIC_READONLY);// 2. normal readonly 호출 : 일반 호출방식 (객체를 만들어서 메모리를 할당한 뒤에 사용 가능)
로켓외부요인 normal =new 로켓외부요인(1000);
Console.WriteLine(normal.날씨흐림_NORMAL_READONLY);}}