구조체(struct)

Soonyoung Kim·2022년 4월 12일
0
post-thumbnail

구조체

  • 구조체는 new 연산자를 사용해 해당 객체를 생성한다.
  • 작성 방법이 클래스와 비슷하다.
// System.DateTime 구조체
DateTime date = new DateTime(2017, 9, 2);
int year = date.Year;
//10일 후를 구한다.
DateTime daysAfter10 = date.AddDays(10);

클래스 vs 구조체

  • 객체를 메모리 상에 저장하는 방식의 차이
    • 클래스 : 변수가 있는 곳과 다른 곳에 객체의 영역이 확보되고 변수에는 참조(메모리 상에 붙여진 주소)가 저장
    • 구조체 : 변수 자체에 객체가 저장
// 클래스
class MyClass{
    public int X { get; set; }
    public int Y { get; set; }
}

// 구조체
struct MyStruct{
    public int X { get; set; }
    public int Y { get; set; }
}

MyClass myClass = new MyClass { X = 1, Y = 2 };
MyStruct myStruct = new MyStruct { X = 1, Y = 2 };

profile
Sin prisa, sin pausa.

0개의 댓글