C#/클래스와 객체지향

C# 클래스와 객체지향 - 21. 구조체

tita 2024. 5. 29. 19:49

구조체는 간단한 객체를 만들 때에 사용하는 형식입니다. 클래스와 거의 동일한 구문을 사용하지만, 복사 형식이 다르고 클래스보다 제한이 조금 많습니다. 또한 구조체는 상속이 불가능하며, 인터페이스를 구현할 수도 없습니다. 대신 클래스보다 안정성 측면에서는 높답니다.

 

 

[구조체 선언]

구조체는 다음과 같이 선업합니다.

struct Point
{
    public int x;
    public int y;
}

 

이렇게 생성한 구조체는 new 키워드를 사용하지 않아도 인스턴스를 생성할 수 있습니다. 

대신 멤버 변수를 별도로 초기화해주어야 합니다.

class Program
{
    struct Point
    {
        public int x;
        public int y;
    }
    
    static void Main(string[] args)
    {
        Point point;
        
        // 초기화 해주어야 함
        point.x = 10;
        point.y = 10;
        
        Console.WriteLine(point.x);
        Console.WriteLine(point.y);
    }
}

 

 

 

 

[구조체의 생성자]

구조체는 생성자를 선언할 때, 클래스와는 다르게 매개변수 없는 생성자를 선언할 수 없습니다.

struct Point
{
    public int x;
    public int y;
    
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

 

구조체는 매개변수 없는 생성자가 자동으로 정의되기 때문입니다.

매개변수는 자동으로 해당 자료형의 기본 값으로 초기화됩니다. 숫자라면 0, 문자열이나 객체라면 null 로 초기화됩니다.

 

구조체는 생성자에서 모든 변수를 초기화해야 합니다.

즉, 선언과 동시에 멤버 변수를 초기화할 수 없습니다.

예제를 통해 살펴보겠습니다.

struct Point
{
    public int x;
    public int y;
    public string testA;
    public string testB;
    // public string testB = "init" -> 이렇게 생성과 동시에 초기화 불가능
    
    // 생성자 오버로딩했습니다.
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y
        
        this.testA = "초기화";
        this.testB = "초기화";
    }
    
    public Point(int x, int y, string test)
    {
        this.x = x;
        this.y = y;
        this.testA = test;
        this.testB = test;
    }
    
    public Point(int x, int y, string testA, string testB)
    {
        this.x = x;
        this.y = y;
        this.testA = testA;
        this.testB = testB;
    }
}

 

 

 

[구조체 복사]

이렇게 생성된 구조체는 값 복사가 이루어집니다.

구조체의 값 복사와 관련된 코드를 살펴보고 끝내겠습니다.

class Program
{
    class PointClass
    {
        public int x;
        public int y;
        
        public PointClass(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
       
    struct PointStruct
    {
        public int x;
        public int y;
        
        public PointStruct(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    
    static void Main(string[] args)
    {
        // 클래스
        PointClass pointClassA = new Point(10, 20);
        PointClass pointclassB = pointClassA; // 참조 복사가 일어납니다.
        
        pointClassB.x = 100;
        pointClassB.y = 200;
        
        Console.WriteLine("pointClassA : " + pointClassA + "," + pointClassA.y);
        Console.WriteLine("pointClassB : " + pointClassB + "," + pointClassB.y);
        Console.WriteLine();      
        
        
        // 구조체
        PointStruct pointStructA = new PointStruct(10, 20);
        PointStruct pointStructB = new PointStructA; // 값 복사가 일어납니다.
        
        pointStructB.x = 100
        pointStructB.y = 200;
        
        Console.WriteLine("pointStructA : " + pointStructA + "," + pointStructA.y);
        Console.WriteLine("pointStructB : " + pointStructB + "," + pointStructB.y);
    }
}