Collections in C#
What is Collection?
Collection is a class used to store set of values. If class contains indexer then it is called as collection. The predefined collectiions are existed in System.Collections namespace. To overcome the drawbacks of array, Collections are used.
Difference Between Arrays and Collections:
|It has fixed size||It has no fixed size|
|Used to store similar types data||Used to store composite or mixed data(int, char & float values)|
|After storing the values, adding new value, editing & deleting old values is very difficult||Collection contains set of methods. By using them adding, editing & deleting of values can be done easily|
If you want to store values & if you want to access them then arrays are best. They will work fast & are very difficult. If you want to store the values & after that if you want to manipulate the values then collections are best.
It is the predefined collection used to store composite or mixed data. when multiple values are stored in collection they will be stored based on index value. The index value starts with zero(0).
1. Example of ArrayList:
Methods in ArrayList collection:
- Add() : It is used to add value to the collection.
- AddRange() : It is used to add set of values to the collection.
- Insert() : It is used to add new value in between existing values.
- Remove() : It is used to delete specified values from collection.
- RemoveAt() : It is used to delete value from collection based on the given index value.
- Clear() : It is used to delete all the values from collection.
- IndexOf() : It is used to get Index value of the specified value in the collection.
- Sort() : It is used to show values in ascending order.
It is the property in ArrayList collection used to get count of values.
2. Example of ArrayList using Property:
It is used to store values in key/value pair. And the values will be sorted based on the given keys.
Example of SortedList:
SortedList collection contains Add() method. By using that method key & value can be given. Count Property in SortedList collection is used to get count of values. GetKey() is used to get key from the collection. GetByIndex() is used to get value from the given index.
This collection will store values in key/value pair and for every key, hash code or hash value is generated. It is used to uniquely identify each value in the collection.
Example of HashTable:
DictionaryEntry is the predefined structure used to access values from HashTable collection. GetHashCode() is used to get hash code for the given key. Key & Value properties in DictionaryEntry is used to access key and value from collection.
It will store value in 'First In Last Out' order.
Example of Stack:
Push() is used to send values to the stack & Pop() is used to delete values from stack collection.
It will store values in 'First In First Out' order.
1) Example of Queue:
Enqueue() is used to send the values to the collection.
Dequeue() is used to delete values from the collection.
In this way, we learned Collections in C# and some basic programs which will help beginners to quick start with Collections.
You may also be interested in...
- Slack Integration with C#
- Xamarin Forms: Getting Started
- Part 1: Introduction to C#
- Part 2: Arrays and Function in C#
- Part 3: OOPs Concepts in C#
- Part 4: Constructor/Destructor in C#
- Part 5: Abstract class in C#
- Part 6: Indexer,Delegates, Anonymous Method, Lambda expression in C#
- Part 7: Collections in C#
- Part 8: Generics in C#
- Part 9: Attributes in C#
- Part 10: Sealed/Partial class in C#
- Part 11: MultiThreading in C#