Share via


CA1023: Indexers should not be multidimensional

TypeName

IndexersShouldNotBeMultidimensional

CheckId

CA1023

Category

Microsoft.Design

Breaking Change

Breaking

Cause

A public or protected type contains a public or protected indexer that uses more than one index.

Rule Description

Indexers, that is, indexed properties, should use a single index. Multi-dimensional indexers can significantly reduce the usability of the library. If the design requires multiple indexes, reconsider whether the type represents a logical data store. If not, use a method.

How to Fix Violations

To fix a violation of this rule, change the design to use a lone integer or string index, or use a method instead of the indexer.

When to Suppress Warnings

Suppress a warning from this rule only after carefully considering the need for the nonstandard indexer.

Example

The following example shows a type, DayOfWeek03, with a multi-dimensional indexer that violates the rule. The indexer can be seen as a type of conversion and therefore is more appropriately exposed as a method. The type is redesigned in RedesignedDayOfWeek03 to satisfy the rule.

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: Use integral or string argument for indexers

CA1024: Use properties where appropriate