기계는 거짓말하지 않는다

C# partial class 본문

C#

C# partial class

KillinTime 2021. 7. 15. 19:29

partial 키워드를 사용하면 클래스, 구조체, 인터페이스 또는 메서드의 정의를 둘 이상의 소스 파일에 분할이 가능하다.

이는 컴파일될 때 결합되고 대규모 프로젝트에서 클래스를 분산하면 여러 사람이 동시에 작업이 가능하다.

Windows Forms, 웹 서비스 코드 작성 등에 유용하다.

public partial class ClassName과 같이 partial 키워드를 붙여 사용한다.

public partial class TempClass
{
    private int num1 { get; set; }
    private int num2 { get; set; }
    private string str1 { get; set; }
    private string str2 { get; set; }

    public TempClass() { }

    public TempClass(int num1, int num2, int num3, string str1 = "ABCD", string str2 = "EFGH", string str3 = "IJKL")
    {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
        this.str1 = str1;
        this.str2 = str2;
        this.str3 = str3;
    }
}

public partial class TempClass
{
    private int num3 { get; set; }
    private string str3 { get; set; }

    public void PrintStr()
    {
        Console.WriteLine($"{str1} {str2} {str3}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        TempClass tc = new TempClass(10, 20, 30);
        tc.PrintStr();
    }
}

 

'C#' 카테고리의 다른 글

C# Windows Forms 이벤트 등록  (0) 2021.07.16
C# is, as, typeof 키워드  (0) 2021.07.16
C# Windows Forms MenuStrip Check  (0) 2021.07.13
C# Windows Forms TextBox 빈 문자 확인  (0) 2021.07.12
C# 소수점 제한, 천 단위 콤마 출력  (0) 2021.07.12
Comments