class Example
{
public int a=0;
}
class Program
{
static void SetValue(Example ex)
{
ex.a = 10;
}
static void Main(string[] args)
{
Example ex = new Example();
SetValue(ex);
Console.WriteLine(ex.a); // 10 출력
}
}
- Class는
reference
type
- HEAP에 할당된다
- 클래스가 할당된 HEAP 메모리의 주소가 STACK 메모리에 추가
- 메모리를 많이 차지하는 경우에 사용
- 복잡한 데이터와 행위를 정의하는 곳에 사용
- 바뀐 값을 출력한다
struct Example
{
public int a=0;
}
class Program
{
static void SetValue(Example ex)
{
ex.a = 10;
}
static void Main(string[] args)
{
Example ex = new Example();
SetValue(ex);
Console.WriteLine(ex.a); // 0 출력
}
}
- Struct는
value
type
- STACK에 할당된다
- 생성 시, 초기값을 설정한다
- 짧게 생성되고 없어지는 경우 사용
- 파라미터를 전달할 때, 데이터를 복사하여 전달
- 상속할 수 없다