Being in development for long period of time, I have ended in repeating same code again and again. Unconsciously I started use list< c# collection> in most of my development.
I used rarely other collections. They were literally not exist to me. So I like to revisit the other collections with you all :)
Dictionary
C# Dictionary is imported from the 'System.Collections.Generic' namespace and used to store generic collection of keys and values pair of data. Key must to unique, it will thrown ArgumentException when attempting to add a duplicate key. And it will thrown KeyNotFoundException when attempting to retrieve value with key which is not present
To initializes a dictionary.
Dictionary<TKey,TValue> dict = new Dictionary<TKey,TValue> ();
To add a element in dictionary
dict.Add(TKey, TValue)
To get an element in dictionary
dict.TryGetValue(TKey, out value)
To get number of items in dictionary
dict.Count
To Remove an item
dict.Remove(Tkey
More on this , Please refer msdn https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-6.0
Queue
A Queue in C# represents a first-in, first-out (FIFO) collection of objects. An example of a queue is a line of people waiting.
The Queue<T> class in the 'System.Collection.Generic namespace represents a queue in C#, where T specifies the type of elements in the queue..
Objects stored in a Queue<T> are inserted at one end and removed from the other. Queues and stacks are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use Queue<T> if you need to access the information in the same order that it is stored in the collection
To initializes a Queue
Queue<T> queue = newQueue<T>();
To get number of items in Queue
queue.Count()
To add a element in Queue
queue.Enqueue(T);
To retrieve and remove element in Queue
queue.Dequeue();
To retrieve without removing element in Queue
queue.Peek();
- Single dimensional array
- Multi dimensional array
- Jagged array
No comments:
Post a Comment