A Interfaces for collections in C #: ICollection
Initialization object method for the collection interface:
Icollection<data type> mycollect=new collection< Data type > ();
Now let's look at an interface for a collection of integer types
Using System.Collections.ObjectMode
You have to have this reference.
icollection<int> mycollection = new collection<int> ();
Mycollection.add (100);//Add Element
Mycollection.add (22);
Mycollection.add (30);
foreach (int x in mycollection)
Console.WriteLine (x); Output elements
Console.WriteLine ("Number of elements in the collection {0}", Mycollection.count);
Mycollection.add (222);
Mycollection.remove (22);//delete element
Console.WriteLine ("Number of elements in the collection {0}", Mycollection.count);
Console.WriteLine (Mycollection.contains (22));//Determine if there is this element in the collection
Whether an element exists in the Interpretation collection: Mycollection.contains (x); some words return 1, otherwise return 0;
Copies the elements of the collection into an array of the same size:
int[] myarray = new Int[mycollection.count];
Mycollection.copyto (myarray, 0)//starting with the first element of mycollection
int[] myarray = new Int[mycollection.count]; Mycollection.copyto (myarray, 0); Starting with the first element of mycollection Console.WriteLine ("Xia now compares elements in two objects"); for (int i = 0; i < Mycollection.count; i++) { Console.Write ("in myarray {0},", Myarray[i]); } foreach (int x in mycollection) Console.WriteLine ("In Mycollection{0}", x); |
|
You can construct other types of generic collections simply by following the methods described above.
Now demonstrate another type of generic collection:
icollection<string> another = new collection<string> (); Constructs a collection of string types Another. ADD ("the"); Another. ADD ("People ' s"); Another. ADD ("Republic"); Another. ADD ("the"); foreach (String str in another) Console.Write (str); |
The output result of this object is: |
Two BitArray
The BitArray class is a bit array, the size of the array is determined when the object is created, each data element can only represent one bit, the value of the element can only be 1 and 0, which is 1 in true, and 0 in False, when the object is initialized with an element of another data type. Then each element will occupy the array space of the type in memory, and the following table provides a special method for this class:
Method name |
The function of the method |
and |
Elements in BitArray perform bitwise AND operations |
Or |
Bitwise OR operation |
Not |
Take Back operations |
Xor |
XOR or operation |
Get/set |
Gets or designs the bit set at a specific location to the specified value |
SetAll |
Sets all the bits in the BitArray to the specified value |
|
Initialize a BitArray class
BitArray Mybitarray = new BitArray (4); Mybitarray[0] = false; Mybitarray[1] = true; MYBITARRAY[2] = true; MYBITARRAY[3] = false; Displaybitarray ("Mybitarray", Mybitarray); Console.WriteLine ("After Not ()"); Mybitarray.not (); Displaybitarray ("Mybitarray", Mybitarray); |
|
Of course, a method is also defined to be used specifically for output:
public static void Displaybitarray (String arraylistname, BitArray Mybitarray)
{for (int i = 0; i < Mybitarray.count i++)
{
Console.WriteLine (Arraylistname + "[" + i + "] =" + mybitarray[i]);
}
}