Collections
Collections is similar to array which is used to store collection of values. Collections provide a more flexible way to work with groups of objects. Unlike arrays, the group of objects you work with can grow and shrink dynamically as the needs of the application change. For some collections, you can assign a key to any object that you put into the collection so that you can quickly retrieve the object by using the key. A collection is a class, so you must declare an instance of the class before you can add elements to that collection.
Collection | Syntax |
---|---|
List | List <datatype> listName = new List<datatype>(); |
ArrayList | ArrayList arraylistName = new ArrayList(); |
Stack | Stack stackName = new Stack(); |
Queue | Queue queueName = new Queue(); |
Dictionary | Dictionary<datatype1, datatype1> dictionaryName = new Dictionary<datatype1, datatype1>(); |
CODE
EG 1 . Writing collections inside main method.
//All Collections except List present inside namespace System.Collections
using System.Collections;
namespace projectName{
    class Program{
        static void Main() {
            //-------- LIST --------
            //<int> is a generic which represents that it can hold only int values inside this List.
            List<int> intList = new List<int>();
            intList.Add(100);
            intList.Add(189);
            intList.Add(165);
            intList.Add(1985);
            intList.Remove(165); //We can remove value from list, this is not possible in ARRAY
            foreach (int eachElement in intList) {
                Console.WriteLine(eachElement);
            }
            Console.WriteLine();// For Empty Line
            //-------- ARRAY LIST --------
            //ArrayList is not having generic so we can store all kind of datatype values.
            ArrayList sampleArrayList = new ArrayList();
            sampleArrayList.Add("CodersCapsule");
            sampleArrayList.Add(23);
            sampleArrayList.Add(3.14);
            sampleArrayList.Add(true);
            sampleArrayList.Add('@');
            //object will convert to specific datatype depending on the value what we trying to store inside.
            foreach (object eachElement in sampleArrayList) {
                Console.WriteLine(eachElement);
            }
            Console.WriteLine();
            //-------- STACK --------
            //In stack what goes first will come last and what goes last will come first(Check Output)
            Stack stackList = new Stack();
            stackList.Push("CodersCapsule");
            stackList.Push(45.654);
            stackList.Push(false);
            foreach (object eachElement in stackList) {
                Console.WriteLine(eachElement);
            }
            Console.WriteLine();
            //-------- QUEUE --------
            //Queue is direct opposite of stack,here what goes first will come first, what goes last will come Last.
            Queue queueList = new Queue();
            queueList.Enqueue(654.364);
            queueList.Enqueue(456);
            queueList.Enqueue("CodersCapsule");
            foreach (object eachElement in queueList) {
                Console.WriteLine(eachElement);
                }
            Console.WriteLine();
            //-------- DICTIONARY --------
            //Dictionary is used to store Key, value pair. <datatype1(key),datatype2(value)>
            Dictionary<int, string> cityAndPincode = new Dictionary<int, string>();
            cityAndPincode.Add(6001,"Chennai");
            cityAndPincode.Add(5001,"Mumbai");
            cityAndPincode.Add(4001,"Bangalore");
            foreach (KeyValuePair<int, string> eachElement in cityAndPincode) {
                Console.WriteLine(eachElement.Key+" : "+eachElement.Value);
            }
        }
    }
}
(Click Here, For more exercises)