다음을 통해 공유


다차원 인덱서는 사용하지 마십시오.

업데이트: 2007년 11월

TypeName

IndexersShouldNotBeMultidimensional

CheckId

CA1023

범주

Microsoft.Design

변경 수준

주요 변경

원인

public 또는 protected 형식에 둘 이상의 인덱스를 사용하는 public 또는 protected 인덱서가 들어 있습니다.

규칙 설명

인덱서, 즉 인덱싱된 속성은 단일 인덱스를 사용해야 합니다. 다차원 인덱서를 사용하면 라이브러리의 유용성이 현저히 줄어들 수 있습니다. 디자인에 여러 인덱스가 필요하면 해당 형식이 논리 데이터 저장소를 나타내는지를 다시 고려해 보아야 합니다. 그렇지 않을 경우에는 메서드를 사용합니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 정수(long) 또는 문자열 인덱스를 사용하도록 디자인을 변경하거나 인덱서 대신 메서드를 사용합니다.

경고를 표시하지 않는 경우

비표준 인덱서의 필요성을 신중하게 고려한 후에만 이 규칙에서 경고를 표시하지 마십시오.

예제

다음 예제에서는 규칙을 위반하는 다차원 인덱서가 있는 DayOfWeek03 형식을 보여 줍니다. 인덱서가 변환 형식으로 보일 수 있으므로 메서드로 노출하는 것이 더 적합합니다. 규칙을 만족하기 위해 형식을 RedesignedDayOfWeek03으로 다시 디자인합니다.

Imports System

Namespace DesignLibrary

    Public Class DayOfWeek03

        Private dayOfWeek(,) As String = {{"Wed", "Thu", "..."}, _
                                          {"Sat", "Sun", "..."}}
                                          ' ...

        Default ReadOnly Property Item(month As Integer, day As Integer) As String
            Get
                Return dayOfWeek(month - 1, day - 1)
            End Get
        End Property

    End Class

    Public Class RedesignedDayOfWeek03

        Private dayOfWeek() As String = _
            {"Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"}
        Private daysInPreviousMonth() As Integer = _
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30}

        Function GetDayOfWeek(month As Integer, day As Integer) As String
            Return dayOfWeek((daysInPreviousMonth(month - 1) + day) Mod 7)
        End Function

    End Class

End Namespace
using System;

namespace DesignLibrary
{
    public class DayOfWeek03
    {
        string[,] dayOfWeek = {{"Wed", "Thu", "..."}, 
                               {"Sat", "Sun", "..."}};
                               // ...

        public string this[int month, int day]
        {
            get
            {
                return dayOfWeek[month - 1, day - 1];
            }
        }
    }

    public class RedesignedDayOfWeek03
    {
        string[] dayOfWeek = 
            {"Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"};

        int[] daysInPreviousMonth = 
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};

        public string GetDayOfWeek(int month, int day)
        {
            return dayOfWeek[(daysInPreviousMonth[month - 1] + day) % 7];
        }
    }
}
using namespace System;

namespace DesignLibrary
{
    public ref class DayOfWeek03
    {
        array<String^, 2>^ dayOfWeek;

    public:
        property String^ default[int,  int]
        {
            String^ get(int month, int day)
            {
                return dayOfWeek[month - 1, day - 1];
            }
        }

        DayOfWeek03()
        {
            dayOfWeek = gcnew array<String^, 2>(12, 7);
            dayOfWeek[0,0] = "Wed";
            dayOfWeek[0,1] = "Thu";
            // ...
            dayOfWeek[1,0] = "Sat";
            dayOfWeek[1,1] = "Sun";
            // ...
        }
    };

    public ref class RedesignedDayOfWeek03
    {
        static array<String^>^ dayOfWeek = 
            {"Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"};

        static array<int>^ daysInPreviousMonth = 
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};

    public:
        String^ GetDayOfWeek(int month, int day)
        {
            return dayOfWeek[(daysInPreviousMonth[month - 1] + day) % 7];
        }
    };
}

관련 규칙

인덱서에 정수 또는 문자열 인수를 사용하십시오.

적합한 속성을 사용하십시오.