CA1023:索引器不应是多维的

类型名

IndexersShouldNotBeMultidimensional

CheckId

CA1023

类别

Microsoft.Design

是否重大更改

原因

公共或受保护类型包含一个公共或受保护索引器,该索引器使用多个索引。

规则说明

索引器(即索引属性)应该使用一个索引。多维索引器会大大降低库的可用性。如果设计需要使用多个索引,请重新考虑类型是否代表逻辑数据存储。否则,请使用方法。

如何解决冲突

要修复与该规则的冲突,请将设计更改为使用长整型或字符串索引,或使用方法来替代索引器。

何时禁止显示警告

仅在认真考虑非标准索引器的需要后,再禁止显示此规则发出的警告。

示例

下面的示例演示类型 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];
        }
    };
}

相关规则

CA1043:将整型或字符串参数用于索引器

CA1024:在适用处使用属性