NameObjectCollectionBase.BaseClear Method
.NET Framework 2.0
Removes all entries from the NameObjectCollectionBase instance.
Namespace: System.Collections.Specialized
Assembly: System (in system.dll)
Assembly: System (in system.dll)
Count is set to zero, and references to other objects from elements of the collection are also released.
This method is an O(1) operation.
The following code example uses BaseClear to remove all elements from a NameObjectCollectionBase.
import System.* ;
import System.Collections.* ;
import System.Collections.Specialized.* ;
public class MyCollection extends NameObjectCollectionBase
{
private DictionaryEntry _de = new DictionaryEntry();
// Gets a key-and-value pair (DictionaryEntry) using an index.
/** @property
*/
public DictionaryEntry get_Item(int index)
{
_de.set_Key(this.BaseGetKey(index));
_de.set_Value(this.BaseGet(index));
return _de ;
} //get_Item
// Adds elements from an IDictionary into the new collection.
public MyCollection(IDictionary d)
{
IDictionaryEnumerator objEnum = d.GetEnumerator();
while (objEnum.MoveNext()) {
DictionaryEntry de = (DictionaryEntry)objEnum.get_Current();
this.BaseAdd(((String)(de.get_Key())), de.get_Value());
}
} //MyCollection
// Clears all the elements in the collection.
public void Clear()
{
this.BaseClear();
} //Clear
} //MyCollection
public class SamplesNameObjectCollectionBase
{
public static void main(String[] args)
{
// 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 (Count = {0}):",
System.Convert.ToString(myCol.get_Count()));
PrintKeysAndValues(myCol);
// Removes all elements from the collection.
myCol.Clear();
Console.WriteLine("After clearing the collection (Count = {0}):",
System.Convert.ToString(myCol.get_Count()));
PrintKeysAndValues(myCol);
} //main
public static void PrintKeysAndValues(MyCollection myCol)
{
for(int i=0 ; i < myCol.get_Count(); i++) {
Console.WriteLine("[{0}] : {1}, {2}",System.Convert.ToString(i),
myCol.get_Item(i).get_Key(), myCol.get_Item(i).get_Value());
}
} //PrintKeysAndValues
} //SamplesNameObjectCollectionBase
/*
This code produces the following output.
Initial state of the collection (Count = 3):
[0] : red, apple
[1] : yellow, banana
[2] : green, pear
After clearing the collection (Count = 0):
*/
Windows 98, Windows Server 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
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: