다음을 통해 공유


매개 변수 디자인

업데이트: 2007년 11월

이 항목의 지침은 멤버 매개 변수에 대한 올바른 형식과 이름을 선택하는 데 도움이 됩니다. 다음 항목에서는 매개 변수에 대한 디자인 지침도 제공합니다.

멤버에 필요한 기능을 제공하는 가장 적게 파생되는 매개 변수 형식을 사용합니다.

다음 코드 예제에서는 이러한 지침을 보여 줍니다. BookInfo 클래스는 Publication 클래스에서 상속합니다. Manager 클래스는 BadGetAuthorBiography 및 GoodGetAuthorBiography 메서드를 구현합니다. BadGetAuthorBiography 메서드는 Publication에 선언되어 있는 멤버만 사용하는 경우에도 BookInfo 개체에 대한 참조를 사용하고 GoodGetAuthorBiography 메서드는 올바른 디자인을 보여 줍니다.

' A Class with some basic information.
Public Class Publication
    Dim Protected authorValue as String
    Dim Protected publicationDateValue as DateTime

    Public Sub new(author as String, publishDate as DateTime)
        Me.authorValue = author
        Me.PublicationDateValue = publishDate
    End Sub

    Public Readonly Property  PublicationDate as DateTime 
        Get
            Return publicationDateValue
        End Get
    End Property

    Public Readonly Property Author as String
        Get 
            Return authorValue
        End Get
    End Property
End Class

' A Class that derives from Publication
Public Class BookInfo 
    Inherits Publication

    Dim isbnValue as String

    Public Sub new(author as string, _
        publishDate as DateTime, _
        isbn as String) 
        MyBase.New(author, publishDate)
        Me.isbnValue = isbn
    End Sub

    Public Readonly Property Isbn as String
        Get 
            Return isbnValue
        End Get
    End Property
End Class

Public Class Manager
    ' This method does not use the Isbn member
    ' so it doesn't need a specialized reference to Books
    Shared Function BadGetAuthorBiography(book as BookInfo) as String
        Dim biography as String = ""
        Dim author as String = book.Author
        ' Do work here.
        Return biography
    End Function

    ' This method shows the correct design.
    Shared Function GoodGetAuthorBiography(item as Publication) as String
        Dim biography as String = ""
        Dim author as String = item.Author
        ' Do work here.
        Return biography
    End Function
// A class with some basic information.
public class Publication
{
    string author;
    DateTime publicationDate;

    public Publication(string author, DateTime publishDate)
    {
        this.author = author;
        this.publicationDate = publishDate;
    }
    public DateTime PublicationDate
    {
        get {return publicationDate;}
    }
    public string Author
    {
        get {return author;}
    }
}

// A class that derives from Publication
public class BookInfo :Publication
{
    string isbn;
    public BookInfo(string author, DateTime publishDate, string isbn) :
            base(author, publishDate)
    {
        this.isbn = isbn;
    }
    public string Isbn
    {
        get {return isbn;}
    }
}

public class Manager
{
    // This method does not use the Isbn member
    // so it doesn't need a specialized reference to Books
    static string BadGetAuthorBiography(BookInfo book)
    {
        string biography = "";
        string author = book.Author;
        // Do work here.
        return biography;

    }
    // This method shows the correct design.
    static string GoodGetAuthorBiography(Publication item)
    {
        string biography = "";
        string author = item.Author;
        // Do work here.
        return biography;
    }

예약된 매개 변수는 사용하지 않습니다.

라이브러리의 다음 버전에서는 추가 매개 변수를 사용하는 새 오버로드를 추가할 수 있습니다.

다음 코드 예제에서는 먼저 이 지침을 위반하는 잘못된 메서드를 보여 준 다음 올바로 디자인된 메서드를 보여 줍니다.

    Public Sub BadStoreTimeDifference (localDate as DateTime, _
        toWhere as TimeZone, _
        reserved as Object)
        ' Do work here.
    End Sub

Public Sub GoodCStoreTimeDifference (localDate as DateTime, _
    toWhere as TimeZone)
    ' Do work here.
End Sub

Public Sub GoodCStoreTimeDifference (localDate as DateTime, _
    toWhere as TimeZone, _
    useDayLightSavingsTime as Boolean)
    ' Do work here.
End Sub
    public void BadStoreTimeDifference (DateTime localDate, 
        TimeZone toWhere, 
        Object reserved)
    {
        // Do work here.
    }

public void GoodCStoreTimeDifference (DateTime localDate, 
    TimeZone toWhere)
{
    // Do work here.
}
public void GoodCStoreTimeDifference (DateTime localDate, 
    TimeZone toWhere, 
    bool useDayLightSavingsTime)
{
    // Do work here.
}

포인터, 포인터 배열 또는 다차원 배열을 매개 변수로 사용하는 공개적으로 노출된 메서드를 사용하지 않습니다.

이러한 고급 기능을 몰라도 대부분의 라이브러리를 사용할 수 있습니다.

오버로드 사이의 매개 변수 순서가 일관되지 않더라도 모든 pass-by-value 및 ref 매개 변수(매개 변수 배열 제외) 뒤에 모든 out 매개 변수를 배치합니다.

이 규칙을 사용하면 메서드 시그니처를 보다 쉽게 이해할 수 있습니다.

멤버를 재정의하거나 인터페이스 멤버를 구현할 때 매개 변수의 이름을 일관되게 지정합니다.

오버로드는 같은 매개 변수 이름을 사용해야 합니다. 따라서 선언 멤버와 같은 매개 변수 이름을 사용해야 합니다. 인터페이스 구현에서는 인터페이스 멤버 시그니처에 정의되어 있는 이름과 같은 이름을 사용해야 합니다.

Portions Copyright 2005 Microsoft Corporation. All rights reserved.

Portions Copyright Addison-Wesley Corporation. All rights reserved.

디자인 지침에 대한 자세한 내용은 2005년에 Addison-Wesley에서 책으로 출간한 Krzysztof Cwalina와 Brad Abrams의 "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries"를 참조하십시오.

참고 항목

기타 리소스

멤버 디자인 지침

클래스 라이브러리 개발을 위한 디자인 지침