Menu

1 / 2

Collections in C#: List, Dictionary, Queue, Stack

DEV Community·AllCoderThings·7 months ago
#ZjosLyLG
#csharp#dotnet#programming#beginners#string
Long article (1721 words) — no AI summary yet.
Reading 0:00
15s threshold

Originally published at https://allcoderthings.com/en/article/csharp-collections-list-dictionary-queue-stack

In C#, collections are used to store multiple values dynamically and process them efficiently. Arrays have fixed size, but collections can grow and shrink as needed. This article covers List<T> , Dictionary<TKey,TValue> (and KeyValuePair<,> ), SortedList<TKey,TValue> , Queue<T> , Stack<T> , HashSet<T> , and LinkedList<T> with examples.

List

List<T> is a dynamically resizable collection that holds items of the same type in order. It provides rich APIs for adding/removing, searching, and sorting.

using System ; using System.Collections.Generic ; using System.Linq ;

var numbers = new List < int > { 5 , 1 , 9 }; numbers . Add ( 3 ); // add at the end numbers . Insert ( 1 , 7 ); // insert at a given index numbers . Remove ( 9 ); // remove by value (first match) numbers . RemoveAll ( x => x % 2 == 1 ); // remove all odd numbers numbers . AddRange ( new [] { 10 , 2 , 8 });

numbers .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.