NameObjectCollectionBase.BaseSet Método

Definición

Establece el valor de una entrada en la instancia NameObjectCollectionBase.

Sobrecargas

BaseSet(Int32, Object)

Establece el valor de la entrada que se encuentra en el índice especificado de la instancia NameObjectCollectionBase.

BaseSet(String, Object)

Establece el valor de la primera entrada con la clave especificada de la instancia NameObjectCollectionBase, si la encuentra; en caso contrario, agrega una entrada con la clave y el valor especificados a la instancia NameObjectCollectionBase.

Ejemplos

En el ejemplo de código siguiente se usa BaseSet para establecer el valor de un elemento específico.

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)

Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs

Establece el valor de la entrada que se encuentra en el índice especificado de la instancia NameObjectCollectionBase.

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

Parámetros

index
Int32

El índice basado en cero de la entrada que se va a establecer.

value
Object

Object que representa el nuevo valor de la entrada que se va a establecer. El valor puede ser null.

Excepciones

La colección es de solo lectura.

index está fuera del intervalo válido de índices de la colección.

Comentarios

Este método es una operación O(1).

Consulte también

Se aplica a

.NET 9 y otras versiones
Producto Versiones
.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)

Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs

Establece el valor de la primera entrada con la clave especificada de la instancia NameObjectCollectionBase, si la encuentra; en caso contrario, agrega una entrada con la clave y el valor especificados a la instancia NameObjectCollectionBase.

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

Parámetros

name
String

Clave String de la entrada que se va a establecer. La clave puede ser null.

value
Object

Object que representa el nuevo valor de la entrada que se va a establecer. El valor puede ser null.

Excepciones

La colección es de solo lectura.

Comentarios

Si la colección contiene varias entradas con la clave especificada, este método establece solo la primera entrada. Para establecer los valores de las entradas posteriores con la misma clave, use el enumerador para recorrer en iteración la colección y comparar las claves.

Este método es una operación O(1).

Consulte también

Se aplica a

.NET 9 y otras versiones
Producto Versiones
.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