DictionaryBase 是什么类?

yfyn 2004-05-06 10:37:09
DictionaryBase 是什么类?有什么功能?有没有例子看一下?
...全文
23 2 点赞 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
CMIC 2004-05-06
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemcollectionsdictionarybaseclasstopic.htm
  • 打赏
  • 举报
回复
CMIC 2004-05-06
abstract的类,实现了IDictionary, ICollection,IEnumerable接口,它的作用是:
提供此基类旨在使实施者创建强类型自定义集合变得更容易。实施者应扩展此基类,而不应创建自己的基类。

using System;
using System.Collections;

public class ShortStringDictionary : DictionaryBase {

public String this[ String key ] {
get {
return( (String) Dictionary[key] );
}
set {
Dictionary[key] = value;
}
}

public ICollection Keys {
get {
return( Dictionary.Keys );
}
}

public ICollection Values {
get {
return( Dictionary.Values );
}
}

public void Add( String key, String value ) {
Dictionary.Add( key, value );
}

public bool Contains( String key ) {
return( Dictionary.Contains( key ) );
}

public void Remove( String key ) {
Dictionary.Remove( key );
}

protected override void OnInsert( Object key, Object value ) {
if ( key.GetType() != Type.GetType("System.String") )
throw new ArgumentException( "key must be of type String.", "key" );
else {
String strKey = (String) key;
if ( strKey.Length > 5 )
throw new ArgumentException( "key must be no more than 5 characters in length.", "key" );
}

if ( value.GetType() != Type.GetType("System.String") )
throw new ArgumentException( "value must be of type String.", "value" );
else {
String strValue = (String) value;
if ( strValue.Length > 5 )
throw new ArgumentException( "value must be no more than 5 characters in length.", "value" );
}
}

protected override void OnRemove( Object key, Object value ) {
if ( key.GetType() != Type.GetType("System.String") )
throw new ArgumentException( "key must be of type String.", "key" );
else {
String strKey = (String) key;
if ( strKey.Length > 5 )
throw new ArgumentException( "key must be no more than 5 characters in length.", "key" );
}
}

protected override void OnSet( Object key, Object oldValue, Object newValue ) {
if ( key.GetType() != Type.GetType("System.String") )
throw new ArgumentException( "key must be of type String.", "key" );
else {
String strKey = (String) key;
if ( strKey.Length > 5 )
throw new ArgumentException( "key must be no more than 5 characters in length.", "key" );
}

if ( newValue.GetType() != Type.GetType("System.String") )
throw new ArgumentException( "newValue must be of type String.", "newValue" );
else {
String strValue = (String) newValue;
if ( strValue.Length > 5 )
throw new ArgumentException( "newValue must be no more than 5 characters in length.", "newValue" );
}
}

protected override void OnValidate( Object key, Object value ) {
if ( key.GetType() != Type.GetType("System.String") )
throw new ArgumentException( "key must be of type String.", "key" );
else {
String strKey = (String) key;
if ( strKey.Length > 5 )
throw new ArgumentException( "key must be no more than 5 characters in length.", "key" );
}

if ( value.GetType() != Type.GetType("System.String") )
throw new ArgumentException( "value must be of type String.", "value" );
else {
String strValue = (String) value;
if ( strValue.Length > 5 )
throw new ArgumentException( "value must be no more than 5 characters in length.", "value" );
}
}

}


public class SamplesDictionaryBase {

public static void Main() {

// Creates and initializes a new DictionaryBase.
ShortStringDictionary mySSC = new ShortStringDictionary();

// Adds elements to the collection.
mySSC.Add( "One", "a" );
mySSC.Add( "Two", "ab" );
mySSC.Add( "Three", "abc" );
mySSC.Add( "Four", "abcd" );
mySSC.Add( "Five", "abcde" );

// Tries to add a value that is too long.
try {
mySSC.Add( "Ten", "abcdefghij" );
}
catch ( ArgumentException e ) {
Console.WriteLine( e.ToString() );
}

// Tries to add a key that is too long.
try {
mySSC.Add( "Eleven", "ijk" );
}
catch ( ArgumentException e ) {
Console.WriteLine( e.ToString() );
}

Console.WriteLine();

// Displays the contents of the collection using the enumerator.
Console.WriteLine( "Initial contents of the collection:" );
PrintKeysAndValues( mySSC );

// Searches the collection with Contains.
Console.WriteLine( "Contains \"Three\": {0}", mySSC.Contains( "Three" ) );
Console.WriteLine( "Contains \"Twelve\": {0}", mySSC.Contains( "Twelve" ) );
Console.WriteLine();

// Removes an element from the collection.
mySSC.Remove( "Two" );

// Displays the contents of the collection using the Keys property.
Console.WriteLine( "New state of the collection:" );
PrintKeysAndValues3( mySSC );

}

// Uses the enumerator.
public static void PrintKeysAndValues( ShortStringDictionary myCol ) {
DictionaryEntry myDE;
System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
while ( myEnumerator.MoveNext() )
if ( myEnumerator.Current != null ) {
myDE = (DictionaryEntry) myEnumerator.Current;
Console.WriteLine( " {0,-5} : {1}", myDE.Key, myDE.Value );
}
Console.WriteLine();
}

// Uses the foreach statement which hides the complexity of the enumerator.
public static void PrintKeysAndValues2( ShortStringDictionary myCol ) {
foreach ( DictionaryEntry myDE in myCol )
Console.WriteLine( " {0,-5} : {1}", myDE.Key, myDE.Value );
Console.WriteLine();
}

// Uses the Keys property and the Item property.
public static void PrintKeysAndValues3( ShortStringDictionary myCol ) {
ICollection myKeys = myCol.Keys;
foreach ( String k in myKeys )
Console.WriteLine( " {0,-5} : {1}", k, myCol[k] );
Console.WriteLine();
}

}


/*
This code produces the following output.

System.ArgumentException: value must be no more than 5 characters in length.
Parameter name: value
at ShortStringDictionary.OnValidate(Object key, Object value)
at System.Collections.DictionaryBase.System.Collections.IDictionary.Add(Object key, Object value)
at SamplesDictionaryBase.Main()
System.ArgumentException: key must be no more than 5 characters in length.
Parameter name: key
at ShortStringDictionary.OnValidate(Object key, Object value)
at System.Collections.DictionaryBase.System.Collections.IDictionary.Add(Object key, Object value)
at SamplesDictionaryBase.Main()

Initial contents of the collection:
One : a
Four : abcd
Three : abc
Two : ab
Five : abcde

Contains "Three": True
Contains "Twelve": False

New state of the collection:
One : a
Four : abcd
Three : abc
Five : abcde

*/

  • 打赏
  • 举报
回复
相关推荐
发帖
C#
加入

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-05-06 10:37
社区公告

让您成为最强悍的C#开发者