本文整理汇总了C#中System.Collections.Specialized.NameObjectCollectionBase.BaseSet方法的典型用法代码示例。如果您正苦于以下问题:C# NameObjectCollectionBase.BaseSet方法的具体用法?C# NameObjectCollectionBase.BaseSet怎么用?C# NameObjectCollectionBase.BaseSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Specialized.NameObjectCollectionBase
的用法示例。
在下文中一共展示了NameObjectCollectionBase.BaseSet方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: return
//引入命名空间
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] );
}
}
}
输出:
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