Namespace: System.Collections.Specialized
Working with Bits:
.Net Framework provides us with two collections namely
BitArray and BitVector32 to manipulate bits.
BitArray is resizable, but not
dynamically resizable collection. One must specify the size to the constructor.
BitVector32 is fixed size collection that
provides the Complex operations over the underlying int32.
One can perform operations like and, not, or, and
exclusive-or on BitArray. BitArray allows the direct access to the bits while
BitVector32 allows you to create masks for each bit then access those bits.
BitArray V = new BitArray(2); BitArray V2 = new BitArray(2);
V[0] = false; V[0]
= false;
V[1] = true; V[1]
= false;
V = V.Xor(V2);
foreach(bool bit in V)
Consolse.WriteLine(V);
Using BitVector32.
BitVector32 bv = new BitVector32();
int Bit1 = bv.CreateMask();
int Bit2 = bv.CreateMask(Bit1);
bv[Bit1] = true;
bv[Bit2] = false;
Bit Packing with BitVector32:
One can create section within a BitVector32 to store small
values. Like
BitVector32.Section section1 =
BitVector32.CreateSection(10);
BitVector32.Section section2 =
BitVector32.CreateSection(50, section1);
BitVector32 packedBits = new
BitVector32(0);
packedBits [section1] = 7;
packedBits [section2] = 41;
Console.WriteLine(packedBits
[section1]);
Console.WriteLine(packedBits
[section2]);
Console.WriteLine(packedBits);
StringCollection Class:
This class is just like ArrayList.
It extends from IList interface. But it allows only strings to be added and retrieved.
It saves the overhead involved in boxing and unboxing.
StringDictionary Class:
StringDictionary is a typed version
of dictionary collection. It can be used just like a HashTable except the Key
and values must be a string.
Case-Insensitive Collection:
.Net Framework provides
Case-Insensitive HashTable and SortedList. They can be created by using the
static methods of the CollectionsUtil class.
HashTable iTable =
CollectionsUtils.CreateCaseInsensitiveHashTable();
SortedList iList =
CollectionsUtils.CreateCaseInsensitiveSortedList();
Culture Invariant Collection:
By default Collections use current threads’
culture, so comparisons depend upon the rule of the current culture for
ordering and uniqueness. Culture-Invariant Collection can be created by passing
in the Invariant implementation of the Comparer interface. Like:
HashTable invTable = new HashTable(StringComparer.InvariantCulture);
SortedList invList = new SortedList
(StringComparer.InvariantCulture);
The NameValueCollection class:
It inherits from abstract NameObjectCollectionBase class. It is similar to
StringDictionary class except that NameValueCollection allows multiple values
against a key and it also supports retrieve of items by index.
NameValueCollection nvc = new
NameValueCollection();
Nvc.Add(“a”, “some value”);
Nvc.Add(“a”, “more value”);
Foreach(string s in
nvc.GetValues(“a”))
{
Console.WriteLine(s);
}
Nvc.Add(“b”, “b value”);
for(int i=0;i<Nvc.Count;i++)
{
for(int
j=0;j<nvc.GetValues(Nvs.GetKey(i));j++)
Console.WriteLine(Nvc[i][j]);
}
Comments