Monday 11 August 2014

Collection in C#

Collection: 
For many applications, you want to create and manage groups of related objects. There are two ways to group objects: by creating arrays of objects, and by creating collections of objects.Arrays are most useful for creating and working with a fixed number of strongly-typed objects. 
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 a new collection before you can add elements to that collection.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
            //using foreach loop on collection
            var objanimal = new List<string>() {"cat","dog","mouse" };
            foreach (var item in objanimal)
            {
                Console.WriteLine(item);
            }
            //output :
            //cat
            //dog
            //mouse

            //using for loop on collection
            var objanimalfor = new List<string>() {"cat","dog","mouse" };
            for (int i = 0; i < objanimalfor.Count; i++)
            {
                Console.WriteLine(objanimalfor[i]+" " );
            }
            //output :
            //cat
            //dog
            //mouse
           

            //you can remove/insert/removeat in the element from the list
            var objanimalrefunction = new List<string>() { "cat", "dog", "mouse" };
            objanimalrefunction.Remove("cat");

            objanimalrefunction.Insert(0, "cow");

            objanimalrefunction.RemoveAt(2);

            foreach (var item in objanimalrefunction)
            {

                Console.WriteLine(item);
            }
            //output :
            //cow
            //dog



//For the type of elements in the List<T>, you can also define your own class. In the following example, the Galaxy class that is used by the List<T> is defined in the code.

            var objstudent = new List<Student>
            {
                new Student{Name="shoeb",Age=24},
                new Student{Name="abdul",Age=25}
            };
            foreach (var item in objstudent)
            {
                Console.WriteLine(item.Name + " " + item.Age);
            }
            //Output :
            //shoeb 24
            //abdul 25
            Console.ReadLine();

        }
        public class Student
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
}

 


System.Collections.Generic Classes

You can create a generic collection by using one of the classes in the System.Collections.Generic namespace. A generic collection is useful when every item in the collection has the same data type. A generic collection enforces strong typing by allowing only the desired data type to be added.
The following table lists some of the frequently used classes of the System.Collections.Generic namespace:
Class
Description
Dictionary<TKey, TValue>
Represents a collection of key/value pairs that are organized based on the key.
List<T>
Represents a list of objects that can be accessed by index. Provides methods to search, sort, and modify lists.
Queue<T>
Represents a first in, first out (FIFO) collection of objects.
SortedList<TKey, TValue>
Represents a collection of key/value pairs that are sorted by key based on the associated IComparer<T> implementation.
Stack<T>
Represents a last in, first out (LIFO) collection of objects.

System.Collections Classes

The classes in the System.Collections namespace do not store elements as specifically typed objects, but as objects of type Object.

Whenever possible, you should use the generic collections in the System.Collections.Generic namespace or the System.Collections.Concurrent namespace instead of the legacy types in the System.Collectionsnamespace.

The following table lists some of the frequently used classes in the System.Collections namespace:
Class
Description
ArrayList
Represents an array of objects whose size is dynamically increased as required.
Hashtable
Represents a collection of key/value pairs that are organized based on the hash code of the key.
Queue
Represents a first in, first out (FIFO) collection of objects.
Stack
Represents a last in, first out (LIFO) collection of objects.

No comments:

Post a Comment