C# collection
C# List
List
is a strongly typed list of objects that can be accessed by index. It can be found under System.Collections.Generic
the namespace. The namespace is automatically included with implicit using.
public List<string> list() { var Summaries = new List<string>(); Summaries.Add("Freezing"); Summaries.Add("Bracing"); Summaries.Add("Chilly"); Summaries.Add("Cool"); Summaries.Add("Mild"); Summaries.Add("Warm"); Summaries.Add("Balmy"); Summaries.Add("Sweltering"); Console.WriteLine(Summaries.Contains("Freezing")); Console.WriteLine(Summaries[1]); Console.WriteLine(Summaries[2]); Summaries.Remove("Freezing"); Summaries.Remove("Bracing"); Console.WriteLine(Summaries.Contains("C#")); Summaries.Insert(4, "Scorching"); Summaries.Sort(); foreach (string Summ in Summaries) { Console.WriteLine(Summ); } return Summaries; }
In the preceding example, we work with the List
collection.
using System.Collections.Generic;
The List
the collection is located in the System.Collections.Generic
namespace.
var Summaries = new List<string>();
A generic dynamic array is created. We specify that we work with strings with the type specified inside <> characters.
Summaries.Add("Freezing"); Summaries.Add("Bracing"); Summaries.Add("Chilly");
We add elements to the List using the Add
method.
Console.WriteLine(Summaries.Contains("Freezing"));
We check if the List contains a specific string using the Contains
method.
Console.WriteLine(Summaries[1]); Console.WriteLine(Summaries[2]);
We access the list's second and third elements of the List using index notation.
Summaries.Remove("Freezing"); Summaries.Remove("Bracing");
We remove two strings from the List.
Summaries.Insert(4, "Scorching");
We insert a string at a specific location.
Summaries.Sort();
C# ArrayList
ArrayList
is a collection from a standard System.Collections
namespace. It is a dynamic array. It provides random access to its elements. An ArrayList
automatically expands as data is added. Unlike arrays, an ArrayList
can hold data of multiple data types. Elements in the ArrayList
are accessed via an integer index. Indexes are zero-based. Indexing of elements and insertion and deletion at the end of the ArrayList
takes constant time. Inserting or deleting an element in the middle of the dynamic array is more costly. It takes linear time.
C# Hashtable
It uses a key to access the elements in the collection. A hash table is used when you need to access elements by using a key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.
C# SortedList
It uses a key as well as an index to access the 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 items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.
C# Stack
It represents a last-in, first-out collection of objects. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.
C# Queue
It represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue and when you remove an item, it is called deque.
C# BitArray
It represents an array of binary representations using the values 1 and 0. It is used when you need to store the bits but do not know the number of bits in advance. You can access items from the BitArray collection by using an integer index, which starts from zero.
C# Dictionary
In c#, Dictionary is a generic type of collection, and it is used to store a collection of key/value pairs organized based on the key. The dictionary in c# will allow to store only the strongly-typed objects, i.e., the key/value pairs of the specified data type. In c#, while storing the elements in the dictionary object, you need to make sure that the keys are unique because the dictionary object will allow us to store duplicate values, but the keys must be unique. The size of the dictionary object will vary dynamically so that you can add or remove elements from the dictionary based on our requirements. In c#, the dictionary object is the same as the hashtable object, but the only difference is the dictionary object is used to store a key-value pair of the same data type elements.