Share via


陣列用法方針

如需陣列和陣列用法的一般描述,請參閱陣列System.Array 類別

陣列和集合的比較

對於何時使用陣列以及何時傳回集合,類別庫設計人員可能需要作困難的決策。雖然這些型別有類似用法模型,但是具有不同效能特性。一般而言,有支援 AddRemove 或其他操作集合的方法時,請使用集合。

如需使用集合的詳細資訊,請參閱集合和資料結構

陣列用法

請不要傳回陣列的內部執行個體。這樣可以藉由呼叫程式碼而變更陣列。下列範例展示陣列 badChars 如何能由存取 Path 屬性的任何程式碼而變更,就算該屬性並沒有實作 set 存取子。

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 陣列並傳回複本,但是這會有很重要的效能含意。如需詳細資訊,請參閱之後的子章節:傳回陣列的屬性。下列程式碼範例將示範如何修改 GetInvalidPathChars 方法,以傳回 badChars 陣列的複製品。

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';

在集合中使用索引屬性

具索引的屬性只能當做集合類別或介面的預設成員使用。請勿在非集合的型別中建立函式系列。類似 AddItemCount 等方法模式,會以信號通知該型別應該是集合。

傳回空陣列

StringArray 屬性應該絕對不會傳回 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.

如需設計方針的詳細資訊,請參閱由 Krzysztof Cwalina 和 Brad Abrams 所著,並由 Addison-Wesley 於 2005 年發行的「Framework 設計方針:可重複使用之 .NET 程式庫的慣例、慣用語法和模式」一書。

請參閱

參考

Array

概念

用法方針

其他資源

開發類別庫的設計方針