1 System. Array Introduction
An Array class is an abstract base class. We cannot create an Array class instance using the following method:
Array myArray=new Array();
However, it provides CreateInstance to build an array.
Array myArray=Array.CreateInstance(typeof(string),10);
2. System. Array Method
BinarySearch uses the Binary Search Method to search for values of a person in a one-dimensional sorting array.
Clear sets a group of elements in the array to 0 or null.
CreateInstance initializes a new instance of the Array class
GetLength specifies the total number of elements in the array with a given dimension
GetLowerBound specifies the lower bound of the given Array
GetUpperBound specifies the upper bound of the given Array
GetValue returns the value of the specified Element in the given array. This element can be specified by specifying a location index.
IndexOf returns the location index of the given value when it first appears in a one-dimensional array.
Sort array elements
3. Introduction to System. Collections
System. Collections is a namespace that provides a set of interfaces and classes, allowing you to perform set operations on a set of data elements.
Class:
Hashtable: stores key-value pairs, which are organized based on key encoding.
SortedList: Organizes objects and allows access to these objects through indexes or key values.
Interface:
ICollection defines the size, enumerator, and synchronization method for all sets.
IEnumerator provides a simple loop and list of the entire set. You can use the IDictionaryEnumerator derived from IEnumerator to enumerate the elements in the dictionary.
A set of objects that are accessed independently by IList through indexes.
Structure:
DictionaryEntry defines dictionary key-value pairs that can be set or retrieved
4. Hashtable class
The Hashtable class stores data as a set of key-value pairs, which are organized based on key encoding.
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. collections; namespace BaseConsole {public partial class HashtableExample: Form {private Hashtable _ hashtable = new Hashtable (); public HashtableExample () {InitializeComponent ();} private void btnAdd_Click (obj Ect sender, EventArgs e) {try {// Add the element to the hash table _ hashtable. add (txtKey. text, txtValue. text);} catch (Exception ex) {MessageBox. show ("produces a system error. "); // Generates an unknown error Application. exit ();} lstAddedKey. items. add (txtKey. text);} private void lstAddedKey_SelectedIndexChanged (object sender, EventArgs e) {string selectedItem = lstAddedKey. selectedItem. toString (); string selectedValue = (string) _ hashtable [selectedItem]; // retrieves the txtSelectedValue object. text = selectedValue ;}}}
Result:
5. ArrayList class
The Array class belongs to the System namespace, And the ArrayList class belongs to the System. Collections namespace.
Attribute:
Capacity: specifies the number of elements that an array list can contain. The default Capacity is 16.
Method:
Contains: checks whether a given element belongs to an array list.
Insert: inserts a given element into the index location specified in the array list.
Remove: removes the specified object that appears for the first time from the array list.
RemoveAt: removes the element at the specified index position in the array list.
TrimToSize: defines the actual number of elements in the array list.
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms;/**/using System. collections; namespace BaseConsole {public partial class frmArrayListExample: Form {private ArrayList _ arrPerson = new ArrayList (); public frmArrayListExample () {InitializeComponent ();} private void btnAdd _ Click (object sender, EventArgs e) {_arrPerson.Add(this.txt PersonName. text); lstMessage. items. clear (); lstMessage. items. add ("the array capacity is:" + this. _ arrPerson. capacity. toString (); lstMessage. items. add ("the number of elements in the array is:" + this. _ arrPerson. count. toString (); lstMessage. items. add ("-------------------------------------------------"); foreach (string personName in _ arrPerson) {lstMessage. items. add ("element:" + personName );} LstMessage. items. add ("---------------------------------------------------"); _ arrPerson. trimToSize (); lstMessage. items. add ("the size of the sorted array is:" + _ arrPerson. capacity. toString ();} private void btnSearch_Click (object sender, EventArgs e) {if (_ arrPerson. contains (txtSearchString. text) {MessageBox. show ("This element exists. ");} Else {MessageBox. Show (" This element does not exist. ");}}}}
Result: