配列の使用方法のガイドライン

配列および配列の使用方法に関する一般的な説明については、「配列」および「Array クラス」を参照してください。

配列とコレクション

クラス ライブラリのデザイナにとって、配列を使用するときと、コレクションを返すときについて判断することは難しいものです。これらの型は、使用方法のモデルは似ていますが、パフォーマンス特性が異なります。一般的に、AddRemove、またはコレクションを操作するその他のメソッドがサポートされている場合、コレクションを使用する必要があります。

コレクションの使い方の詳細については、「コレクションとデータ構造体」を参照してください。

配列の使用法

配列の内部インスタンスを返さないでください。これによって、配列を変更するコードが呼び出せるようになります。プロパティが set アクセサを実装していない場合でも、Path プロパティにアクセスする任意のコードによって配列 badChars を変更する例を次に示します。

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class ExampleClass
   NotInheritable Public Class Path
      Private Sub New()
      End Sub

      Private Shared badChars() As Char = {Chr(34),"<"c,">"c}
      
      Public Shared Function GetInvalidPathChars() As Char()
         Return badChars
      End Function

   End Class
   
   Public Shared Sub Main()
      ' The following code displays the elements of the 
      ' array as expected.
      Dim c As Char
      For Each c In  Path.GetInvalidPathChars()
         Console.Write(c)
      Next c
      Console.WriteLine()
      
      ' The following code sets all the values to A.
      Path.GetInvalidPathChars()(0) = "A"c
      Path.GetInvalidPathChars()(1) = "A"c
      Path.GetInvalidPathChars()(2) = "A"c
      
      ' The following code displays the elements of the array to the
      ' console. Note that the values have changed.
      For Each c In  Path.GetInvalidPathChars()
         Console.Write(c)
      Next c
   End Sub
End Class
using System;
using System.Collections;

public class ExampleClass
{
   public sealed class Path
   {
      private Path(){}
      private static char[] badChars = {'\"', '<', '>'};
      public static char[] GetInvalidPathChars()
      {
         return badChars;
      }
   }
   public static void Main()
   {
      // The following code displays the elements of the 
      // array as expected.
      foreach(char c in Path.GetInvalidPathChars())
      {
         Console.Write(c);
      }
      Console.WriteLine();

      // The following code sets all the values to A.
      Path.GetInvalidPathChars()[0] = 'A';
      Path.GetInvalidPathChars()[1] = 'A';
      Path.GetInvalidPathChars()[2] = 'A';

      // The following code displays the elements of the array to the
      // console. Note that the values have changed.
      foreach(char c in Path.GetInvalidPathChars())
      {
         Console.Write(c);
      }
   }
}

前の例は、badChars 配列を readonly (Visual Basic では ReadOnly) にすることでは問題を修正できません。badChars 配列の複製を作成し、コピーを戻すことはできますが、これはパフォーマンスにかなり影響します。詳細については、以下の「配列を返すプロパティ」を参照してください。badChars 配列の複製を返すように GetInvalidPathChars メソッドを変更する方法を次のコード例に示します。

Public Shared Function GetInvalidPathChars() As Char()
   Return CType(badChars.Clone(), Char())
End Function
public static char[] GetInvalidPathChars()
{
   return (char[])badChars.Clone();
}

配列を返すプロパティ

配列を返すプロパティによるコードの非効率化を避けるには、コレクションを使用します。次のコード例では、myObj プロパティが呼び出されるたびに、配列のコピーが作成されます。結果として、次のループの中で、2n+1 個の配列のコピーが作成されることになります。

Dim i As Integer
For i = 0 To obj.myObj.Count - 1
   DoSomething(obj.myObj(i))
Next i
for (int i = 0; i < obj.myObj.Count; i++)
      DoSomething(obj.myObj[i]);

詳細については、「プロパティとメソッドの選択」を参照してください。

配列を返すフィールド

readonly (Visual Basic では ReadOnly) の配列のフィールドは使用しないでください。このフィールドを使用すると、配列は読み取り専用となって変更できません。ただし、配列内の要素は変更できます。読み取り専用の配列 InvalidPathChars の要素を変更する方法を次のコード例に示します。

public sealed class Path
{
   private Path(){}
   public static readonly char[] InvalidPathChars = {'\"', '<', '>','|'}'
}
//The following code can be used to change the values in the array.
Path.InvalidPathChars[0] = 'A';

コレクションでのインデックス付きプロパティの使用

インデックス付きプロパティは、コレクション クラスまたはインターフェイスの既定のメンバとしてだけ使用します。関数のファミリをコレクション型以外で作成しないでください。AddItem、および Count などのメソッドのパターンによって、型をコレクションにする必要があることが示されます。

空の配列の戻り値

String プロパティと Array プロパティでは、null 参照を返しません。このようなコンテキストで Null を理解することは困難です。たとえば、次のコードが機能すると開発者が見なしてしまう可能性があります。

Public Sub DoSomething()
   Dim s As String = SomeOtherFunc()
   If s.Length > 0 Then
      ' Do something else.
   End If
End Sub
public void DoSomething()
{
   string s = SomeOtherFunc();
   if (s.Length > 0)
   {
      // Do something else.
   }
}

一般的な規則として、null、空の文字列 ("")、および空の (項目が 0 の) 配列は、同じ方法で処理できる必要があります。null 参照の代わりに、空の配列を返してください。

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』を参照してください。

参照

関連項目

Array

概念

使用方法のガイドライン

その他の技術情報

クラス ライブラリ開発のデザイン ガイドライン