请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
 Add 方法
全部折叠/全部展开 全部折叠
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
Dictionary.Add 方法

注意:此方法在 .NET Framework 2.0 版中是新增的。

将指定的键和值添加到字典中。

命名空间:System.Collections.Generic
程序集:mscorlib(在 mscorlib.dll 中)

Visual Basic(声明)
Public Sub Add ( _
    key As TKey, _
    value As TValue _
)
Visual Basic(用法)
Dim instance As Dictionary(Of TKey, TValue)
Dim key As TKey
Dim value As TValue

instance.Add(key, value)
C#
public void Add (
    TKey key,
    TValue value
)
C++
public:
virtual void Add (
    TKey key, 
    TValue value
) sealed
J#
public final void Add (
    TKey key, 
    TValue value
)
JScript
public final function Add (
    key : TKey, 
    value : TValue
)

参数

key

要添加的元素的键。

value

要添加的元素的值。对于引用类型,该值可以为 空引用(在 Visual Basic 中为 Nothing)。

异常类型条件

ArgumentNullException

key 为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentException

Dictionary 中已存在具有相同键的元素。

通过设置 Dictionary 中不存在的键值(例如,myCollection[myKey] = myValue,在 Visual Basic 中为 myCollection(myKey) = myValue),还可以使用 Item 属性添加新元素。但是,如果指定的键已经存在于 Dictionary 中,则设置 Item 属性将改写旧值。相比之下,如果所指定的键的值已存在,Add 方法则会引发异常。

如果 Count 属性值已经等于容量,则会通过自动重新分配内部数组来增加 Dictionary 的容量,并在添加新元素之前将现有元素复制到新数组中。

键不能为 空引用(在 Visual Basic 中为 Nothing),但是如果 TValue 为引用类型,值则可以为空。

如果 Count 小于容量,则此方法是一个 O(1) 运算。如果必须增加容量以容纳新元素,则此方法成为 O(n) 运算,其中 nCount

下面的代码示例创建一个空的带有字符串键的字符串 Dictionary,并使用 Add 方法添加一些元素。该示例演示在尝试添加重复的键时 Add 方法引发 ArgumentException

此代码示例摘自一个为 Dictionary 类提供的更大的示例。

Visual Basic
' Create a new dictionary of strings, with string keys.
'
Dim openWith As New Dictionary(Of String, String)

' Add some elements to the dictionary. There are no 
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

' The Add method throws an exception if the new key is 
' already in the dictionary.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try
C#
// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> openWith = 
    new Dictionary<string, string>();

// Add some elements to the dictionary. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is 
// already in the dictionary.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0

.NET Compact Framework

受以下版本支持:2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2010 Microsoft Corporation 版权所有。 保留所有权利 | 商标 | 隐私权声明
Page view tracker