NameObjectCollectionBase.BaseSet 方法

定義

設定 NameObjectCollectionBase 執行個體中的項目值。

多載

BaseSet(Int32, Object)

設定 NameObjectCollectionBase 執行個體指定索引處之項目的值。

BaseSet(String, Object)

設定 NameObjectCollectionBase 執行個體中具有指定索引鍵之第一個項目的值 (如果有找到),否則將具有指定索引鍵和值的項目加入 NameObjectCollectionBase 執行個體。

範例

下列程式代碼範例會使用 BaseSet 來設定特定元素的值。

C#
using System;
using System.Collections;
using System.Collections.Specialized;

public class MyCollection : NameObjectCollectionBase  {

   // Gets or sets the value at the specified index.
   public Object this[ int index ]  {
      get  {
         return( this.BaseGet( index ) );
      }
      set  {
         this.BaseSet( index, value );
      }
   }

   // Gets or sets the value associated with the specified key.
   public Object this[ String key ]  {
      get  {
         return( this.BaseGet( key ) );
      }
      set  {
         this.BaseSet( key, value );
      }
   }

   // Gets a String array that contains all the keys in the collection.
   public String[] AllKeys  {
      get  {
         return( this.BaseGetAllKeys() );
      }
   }

   // Adds elements from an IDictionary into the new collection.
   public MyCollection( IDictionary d )  {
      foreach ( DictionaryEntry de in d )  {
         this.BaseAdd( (String) de.Key, de.Value );
      }
   }
}

public class SamplesNameObjectCollectionBase  {

   public static void Main()  {

      // Creates and initializes a new MyCollection instance.
      IDictionary d = new ListDictionary();
      d.Add( "red", "apple" );
      d.Add( "yellow", "banana" );
      d.Add( "green", "pear" );
      MyCollection myCol = new MyCollection( d );
      Console.WriteLine( "Initial state of the collection:" );
      PrintKeysAndValues2( myCol );
      Console.WriteLine();

      // Sets the value at index 1.
      myCol[1] = "sunflower";
      Console.WriteLine( "After setting the value at index 1:" );
      PrintKeysAndValues2( myCol );
      Console.WriteLine();

      // Sets the value associated with the key "red".
      myCol["red"] = "tulip";
      Console.WriteLine( "After setting the value associated with the key \"red\":" );
      PrintKeysAndValues2( myCol );
   }

   public static void PrintKeysAndValues2( MyCollection myCol )  {
      foreach ( String s in myCol.AllKeys )  {
         Console.WriteLine( "{0}, {1}", s, myCol[s] );
      }
   }
}


/*
This code produces the following output.

Initial state of the collection:
red, apple
yellow, banana
green, pear

After setting the value at index 1:
red, apple
yellow, sunflower
green, pear

After setting the value associated with the key "red":
red, tulip
yellow, sunflower
green, pear

*/

BaseSet(Int32, Object)

來源:
NameObjectCollectionBase.cs
來源:
NameObjectCollectionBase.cs
來源:
NameObjectCollectionBase.cs

設定 NameObjectCollectionBase 執行個體指定索引處之項目的值。

C#
protected void BaseSet (int index, object value);
C#
protected void BaseSet (int index, object? value);

參數

index
Int32

要設定之項目的以零為基底的索引。

value
Object

Object,代表要設定之項目的新值。 這個值可以是 null

例外狀況

集合是唯讀的。

index 超出集合索引的有效範圍。

備註

這個方法是 O (1) 作業。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

BaseSet(String, Object)

來源:
NameObjectCollectionBase.cs
來源:
NameObjectCollectionBase.cs
來源:
NameObjectCollectionBase.cs

設定 NameObjectCollectionBase 執行個體中具有指定索引鍵之第一個項目的值 (如果有找到),否則將具有指定索引鍵和值的項目加入 NameObjectCollectionBase 執行個體。

C#
protected void BaseSet (string name, object value);
C#
protected void BaseSet (string? name, object? value);

參數

name
String

要設定之項目的 String 索引鍵。 這個索引鍵可以是 null

value
Object

Object,代表要設定之項目的新值。 這個值可以是 null

例外狀況

集合是唯讀的。

備註

如果集合包含具有指定索引鍵的多個專案,這個方法只會設定第一個專案。 若要設定具有相同索引鍵的後續專案值,請使用列舉值逐一查看集合,並比較索引鍵。

這個方法是 O (1) 作業。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0