Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, April 22, 2017

What is Collections


Collections is a group of objects. The .NET Framework contains a large number of interfaces and classes that define and implement various types of collections.
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, 
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.

collections is a namespace .It has inbuilt classes.Collections simplify many programming tasks because they provide inbuilt solutions to several common, but sometimes tedious-to-develop, data structures. For example, there are built-in collections that support dynamic arrays, linked lists, stacks,queues, and hash tables.
If developer writes a program for sorting. it will increase size of metadata, so CLR will slow execution time. Same task can be done by using collections.

when we use collection class like this,these classes hold data internally object data type or collection based classes uses object data type.
collections supports boxing and unboxing.
collections supports object data type.

Uses Of Collections
Collection classes are specialized classes for data storage and retrieval.Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items on the basis of an index etc.

The following lists some of the frequently used classes in the System.Collections namespace: 
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.
ArrayList
The ArrayList class supports dynamic arrays, which can grow or shrink as needed. In C#,standard arrays are of a fixed length, which cannot be changed during program execution.This means you must know in advance how many elements an array will hold. But
sometimes you may not know until runtime precisely how large an array you will need.
To handle this situation, use ArrayList.

Program 1
Step 1 - We will create  arraylist and add 6 elements to it.
Step 2 - By using for loop, we will write all elements.
Step 3- We will remove 2 elements.

using System;
using System.Collections;

class Program
{
    static void Main(string[] args)
    {
        //Adding element to ArrayList
        ArrayList al = new ArrayList();
        al.Add("Element1");
        al.Add("Element2");
        al.Add("Element3");
        al.Add("Element4");
        al.Add("Element5");
        al.Add("Element6");

        Console.WriteLine("Current elements: ");
        for (int i = 0; i < al.Count; i++)
            Console.Write(al[i] + "\n");
        Console.WriteLine("\n");
        Console.WriteLine("After removing 2 elements");

        // Remove elements from the array list.
        al.Remove("Element2");
        al.Remove("Element4");

        for (int i = 0; i < al.Count; i++)
            Console.Write(al[i] + "\n");
        Console.ReadLine();
    }

}

Output


























Program 2 
Step 1 - We will create an  Arraylist and add integer value to it.
Step 2 - We will sort the arraylist.
Step 3 - we will use binarysearch to find index of element.

using System;
using System.Collections;

class Program
{
    static void Main(string[] args)
    {
        // Create an array list.
        ArrayList al = new ArrayList();
        // Add elements to the array list.
        al.Add(10);
        al.Add(12);
        al.Add(-4);
        al.Add(4);
        al.Add(8);
        Console.Write("Original contents: ");
        foreach (int i in al)
            Console.Write(i + " ");
        Console.WriteLine("\n");
        // Sort
        al.Sort();
        // Use foreach loop to display the list.
        Console.Write("Contents after sorting: ");
        foreach (int i in al)
            Console.Write(i + " ");
        Console.WriteLine("\n");
        Console.WriteLine("Index of 4 is " +
        al.BinarySearch(4));
        // Console.ReadLine();
        Console.ReadLine();
    }
}

Output




























Program 3 - HashTable
Step 1  we will create a hash table.
Step 2 we will add elements to the table.
Step 3 we will get a collection of the keys.
Step 4 we will use the keys to obtain the values.

using System;
using System.Collections;

class Program
{
    static void Main(string[] args)
    {
        // Create a hash table.
        Hashtable ht = new Hashtable();
        // Add elements to the table.
        ht.Add("a", "apple");
        ht.Add("b", "book");
        ht.Add("c", "cat");
        // Get a collection of the keys.
        ICollection c = ht.Keys;
        // Use the keys to obtain the values.
        foreach (string str in c)
            Console.WriteLine(str + ": " + ht[str]);
        Console.ReadLine();
    }
}

Output



You may also like

No comments :

Post a Comment