al . Add ( 99 ); Console . WriteLine ( "nDung lượng của ArrayList: {0} " , al . Capacity ); Console . WriteLine ( "Số phần tử trong ArrayList: {0}" , al . Count ); Console . WriteLine ( "nHiển thị các phần tử có trong ArrayList: " ); foreach ( int i in al ) { Console . Write ( i + " " ); } Console . WriteLine (); Console . WriteLine ( "nSắp xếp ArrayList rồi in: " ); al . Sort (); foreach ( int i in al ) { Console . Write ( i + " " ); } Console . WriteLine (); Console . ReadKey (); } } }
When running the above code, you get the following result:
Example illustrating ArrayList in C #
---------------------------------
Insert some elements into the ArrayList:
Capacity of ArrayList: 8
Element number in ArrayList: 8
Show elements contained in ArrayList:
50 93 85 96 82 25 90 99
Sort ArrayList and print:
25 50 82 85 90 93 96 99
The Hashtable class in C # uses a key-value pair to access elements in this collection. Hash Table is used when you need access to elements using a key, and you can identify a useful key value. Each item in Hash Table has a key and value pair. Key is used to access items in this collection form.
Methods and properties of the Hashtable class in C #:
The table below lists the commonly used properties of the Hashtable class in C #:
Attribute DescriptionCount Get the number of key pairs and values contained in the hashtable. IsFixedSize Get a value indicating whether or not Hashtable has a fixed size. IsReadOnly Get a value indicating whether or not Hashtable is read-only. Item Retrieves or sets the value associated with the specified key. Keys Get an ICollection containing the key in Hashtable. Values Get an ICollection containing the values in the Hashtable.List of commonly used methods of HashTablet class in C #:
Example of Hashtable in C #:
using System ; using System . Collections ; namespace QTMCSharp { class Tester { static void Main ( string [] args ) { Hashtable ht = new Hashtable (); ht . Add ( "001" , "Hoàng Anh" ); ht . Add ( "002" , "Ngọc Anh" ); ht . Add ( "003" , "Mai Anh" ); ht . Add ( "004" , "Trâm Anh" ); ht . Add ( "005" , "Thế Anh" ); ht . Add ( "006" , "Lan Anh" ); ht . Add ( "007" , "Minh Anh" ); ht . Add ( "008" , "Linh Anh" );
if ( ht . ContainsValue ( "Ngân Anh" )) { Console . WriteLine ( "Tên học sinh này đã có trong danh sách" ); } else { ht . Add ( "009" , "Ngân Anh" ); } // Lấy tập hợp các key. ICollection key = ht . Keys ; foreach ( string k in key ) { Console . WriteLine ( k + ": " + ht [ k ]); } Console . ReadKey (); } } }
Running the above program you will get the following result:
007: Minh Anh
004: Tram Anh
005: The Anh
008: Linh Anh
009: Ngan Anh
002: Ngoc Anh
003: Mai Anh
001: Hoang Anh
006: Lan Anh
The SortedList class in C # uses a key as well as an index to access items in a list.
A sorted list is a combination of an array and a Hash Table. It contains a list of items that can be accessed using a key or an index. If you access the item with the index, it is an ArrayList, and if you access the item by key, it is a HashTable. The set of items is always sorted by key value.
Methods and properties of SortedList class in C #:
These are the commonly used properties of SortedList class in C #:
Commonly used methods of SortedList class in C #:
Example of SortedList in C #:
using System ; using System . Collections ; namespace CollectionsApplication { class Program { static void Main ( string [] args ) { SortedList sl = new SortedList (); sl . Add ( "001" , "Hoàng Anh" ); sl . Add ( "002" , "Ngọc Anh" ); sl . Add ( "003" , "Mai Anh" ); sl . Add ( "004" , "Trâm Anh" ); sl . Add ( "005" , "Thế Anh" ); sl . Add ( "006" , "Lan Anh" ); sl . Add ( "007" , "Minh Anh" ); sl . Add ( "008" , "Linh Anh" );
if ( sl . ContainsValue ( "Ngân Anh" )) { Console . WriteLine ( "Tên học sinh này đã có trong danh sách" ); } else { sl . Add ( "009" , "Ngân Anh" ); } // Lấy tập hợp các key. ICollection key = sl . Keys ; foreach ( string k in key ) { Console . WriteLine ( k + ": " + sl [ k ]); } } } }
Running the above code we get the result:
001: Hoang Anh
002: Ngoc Anh
003: Mai Anh
004: Tram Anh
005: The Anh
006: Lan Anh
007: Minh Anh
008: Linh Anh
009: Ngan Anh
The Stack class in C # represents a Last-in, First-out set of objects. It is used when you need access to Last-in, First-out items. When you add an item to the list, it is called pushing and when you remove an item, it is called popping.
Methods and properties of the Stack class in C #:
Stack feature in C #:
Commonly used methods of the Stack class in C #:
Example of Stack in C #:
using System ; using System . Collections ; namespace CollectionsApplication { class Program { static void Main ( string [] args ) { Stack st = new Stack (); st . Push ( 'M' ); st . Push ( 'T' ); st . Push ( 'Q' ); st . Push ( 'W' ); Console . WriteLine ( "1 - Stack hiện tại: " ); foreach ( char c in st ) { Console . Write ( c + " " ); } Console . WriteLine (); st . Push ( 'V' ); st . Push ( 'H' ); Console . WriteLine ( "Giá trị có thể pop tiếp theo trong stack: {0}" , st . Peek ()); Console . WriteLine ( "2 - Stack hiện tại: " ); foreach ( char c in st ) { Console . Write ( c + " " ); } Console . WriteLine (); Console . WriteLine ( "Xóa các giá trị." ); st . Pop (); st . Pop (); st . Pop (); Console . WriteLine ( "3 - Stack hiện tại: " ); foreach ( char c in st ) { Console . Write ( c + " " ); } } } }
Results when running the above code:
1 - Stack current:
WQTM
The value can pop next in the stack: H
2 - Stack current:
HVWQTM
Delete values.
3 - Stack current:
QTM
The Queue class in C # represents a First-in, First-out collection of objects. It is used when you need access to First-in, First-out items. When you add an item to the list, it is called enqueue and when you remove an item, it is called deque.
Method and properties of Queue class in C #:
The commonly used feature of the Queue class in C # is count, which takes the number of elements in the queue.
Commonly used methods of Queue class in C # include:
Example of Queue in C #:
using System ; using System . Collections ; namespace CollectionsApplication { class Program { static void Main ( string [] args ) { Queue q = new Queue (); q . Enqueue ( 'Q' ); q . Enqueue ( 'T' ); q . Enqueue ( 'M' ); q . Enqueue ( 'W' ); Console . WriteLine ( "1 - Queue hiện tại: " ); foreach ( char c in q ) Console . Write ( c + " " ); Console . WriteLine (); q . Enqueue ( 'V' ); q . Enqueue ( 'H' ); Console . WriteLine ( "2 - Queue hiện tại: " ); foreach ( char c in q ) Console . Write ( c + " " ); Console . WriteLine (); Console . WriteLine ( "Xóa vài giá trị!" ); char ch = ( char ) q . Dequeue (); Console . WriteLine ( "Giá trị đã xóa: {0}" , ch ); ch = ( char ) q . Dequeue (); Console . WriteLine ( "Giá trị đã xóa: {0}" , ch ); Console . ReadKey (); } } }
The result when running the above code will be:
1 - Current Queue:
QTMW
2 - Current Queue:
QTMWVH
Delete some values!
Deleted value: Q
Deleted value: T
The BitArray class in C # manages a small array of bit values, represented in binary form, with true that the bit is turned on (1) and false indicating the bit is turned off (0). It is used when you need to store the Bit but does not know how many Bit. You can access items from the BitArray collection using an integer index, starting at 0.
Methods and Properties of the BitArray class in C #:
List of commonly used properties of BitArray class in C #:
The table below lists the commonly used methods of the BitArray class in C #:
Example of BitArray in C #:
using System ; using System . Collections ; namespace Quantrimangcom { class Program { static void Main ( string [] args ) { //Tạo 2 mảng bit có kích thước là 8 BitArray ba1 = new BitArray ( 8 ); BitArray ba2 = new BitArray ( 8 ); byte [] a = { 60 }; byte [] b = { 13 }; //Lưu trữ giá trị 60 và 13 trong các mảng ba1 = new BitArray ( a ); ba2 = new BitArray ( b ); //Hiển thị nội dung của mảng ba1 Console . WriteLine ( "Phần tử của Bit Array ba1: 60" ); for ( int i = 0 ; i < ba1 . Count ; i ++) { Console . Write ( "{0, -6} " , ba1 [ i ]); } Console . WriteLine (); //Hiển thị nội dung của mảng ba2 Console . WriteLine ( "Phần tử của Bit Array ba2: 13" ); for ( int i = 0 ; i < ba2 . Count ; i ++) { Console . Write ( "{0, -6} " , ba2 [ i ]); } Console . WriteLine (); BitArray ba3 = new BitArray ( 8 ); ba3 = ba1 . And ( ba2 ); //Hiển thị nội dung của mảng ba3 Console . WriteLine ( "Bit Array ba3 sau khi thực hiện AND: 12" ); for ( int i = 0 ; i < ba3 . Count ; i ++) { Console . Write ( "{0, -6} " , ba3 [ i ]); } Console . WriteLine (); ba3 = ba1 . Or ( ba2 ); //content of ba3 Console . WriteLine ( "Bit Array ba3 sau khi thực hiện OR: 61" ); for ( int i = 0 ; i < ba3 . Count ; i ++) { Console . Write ( "{0, -6} " , ba3 [ i ]); } Console . WriteLine (); Console . ReadKey (); } } }
After running the above code we can:
Element of Bit Array three: 60
False False True True True True False False
Element of Bit Array three: 13
False True False True False False False
Bit Array ba3 after executing AND: 12
False False True False False False True False
Bit Array ba3 after executing OR: 61
False True False True False False False
According to Tutorialspoint
Previous article: Event (Event) in C #
Next article: Generic in C #