CA1023: Indexery by neměly být multidimenzionální
Název_typu |
IndexersShouldNotBeMultidimensional |
CheckId |
CA1023 |
Kategorie |
Microsoft.design |
Změnit rozdělení |
Rozdělení |
Příčina
Typ veřejné nebo chráněné obsahuje veřejné nebo chráněné indexeru, který používá více než jeden rejstřík.
Popis pravidla
Indexování je indexované vlastnosti, použijte jeden rejstřík.Vícerozměrná indexování lze výrazně snížit použitelnosti knihovny.Jestliže návrh vyžaduje více rejstříků, zvážit, zda typ představuje logické datové úložiště.Pokud tomu tak není, použijte metodu.
Jak opravit porušení
Oprava porušení tohoto pravidla změnit návrh osamoceného celé číslo nebo řetězec indexu nebo pomocí metody namísto službu indexování.
Při potlačení upozornění
Potlačit varování od tohoto pravidla pouze po pečlivě berouc v úvahu potřebu nestandardní indexovací člen.
Příklad
Následující příklad ukazuje typ, DayOfWeek03, s multidimenzionální indexovacího členu, který porušuje pravidlo.Službu indexování lze vnímat jako typ převodu a proto je lépe vystavena jako metoda.Typ je upraveno v RedesignedDayOfWeek03 k tomuto pravidlu odpovídat.
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];
}
};
}
Souvisejících pravidel
CA1043: Použijte celočíselný nebo řetězcový argument pro indexery