반응형 ValueType2 C# readonly 사용시 값 타입 vs 참조 타입 차이점 readonly 사용 시 값 타입 vs 참조 타입readonly 의미값 타입(int, struct 등) : 해당 값 자체를 변경할 수 없음참조 타입(class, List, LinkedList 등) : 참조 자체를 변경할 수 없음 (참조 대상의 내용은 변경 가능) 내부 값 변경값 타입(int, struct 등) : 불가능참조 타입(class, List, LinkedList 등) : 가능 (속성, 메서드 등 사용해서 내부 조작 가능)readonly 값 타입(Value Type) 실제 예제readonly int a = 5;a = 10; // 컴파일 에러a 자체가 값 타입이기 때문에 값 변경 자체가 불가능readonly 참조 타입(Reference Type) 실제 예제readonly List list = new.. 2025. 6. 16. C# String은 참조 타입(Reference type)이면서 불변(Immutable)하다. string vs StringBuilder 예를 들어, int의 경우를 살펴보자. int는 값 타입(value type)이면서 가변(mutable)이다. 값 타입은 데이터 변경 시에 새로운 메모리 할당이 일어나지 않는다. 이미 생성된 메모리의 데이터가 변경된다. static void Main(string[] args) { int a = 100; int b = a; a = 300; Console.WriteLine($"a : {a}"); // 300 Console.WriteLine($"b : {b}"); // 100 } string의 경우를 살펴보자. string은 참조 타입(reference type)이면서 불변(immutable)이다. static void Main(string[] args) { stri.. 2024. 1. 10. 이전 1 다음