数组使用指南

有关数组和数组用法的一般说明,请参见 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 引用。

部分版权所有 2005 Microsoft Corporation。 保留所有权利。

部分版权所有 Addison-Wesley Corporation。 保留所有权利。

设计指引的详细信息,请参阅"框架设计准则: 公约、 成语和可重复使用的模式。网络图书馆"书 Krzysztof Cwalina 和布拉德 · 艾布拉姆斯,2005年艾迪生 - 韦斯利,发表。

请参见

参考

Array

概念

使用准则

其他资源

类库开发的设计准则