C# String Array, Integer Array, Array List and Dictionary

Array

An Array is collection of elements. Arrays are strongly typed collections of the same datatypes and these arrays ar a fixed length that cannot be changed during runtime.

Array List

Array lists are not strongly typed collections. They will store values of different datatypes or the same datatype. Array list size will increase or decrease dynamically; it can take any size of values from any data type.

Dictionary

A Dictionary class is a data structure that represents a collection of keys and value pairs of data. The key is identical in a key-value pair and it can have at most one value in the dictionary

Example Program

  1. using System.IO;  
  2. using System;  
  3. using System.Collections;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Text;  
  7.   
  8. class Program   
  9. {  
  10.     static void Main()   
  11.     {  
  12.   
  13.         //Declaration of Random   
  14.         Random ObjRandom = new Random();  
  15.         var v = ObjRandom.Next(0, 5);  
  16.   
  17.         Console.WriteLine("Random Number is");  
  18.         Console.WriteLine(v);  
  19.   
  20.         //Declaration of String Array  
  21.         string[] ObjArray = new string[5];  
  22.         ObjArray[0] = "Rajesh";  
  23.         ObjArray[1] = "Gonugunta";  
  24.         ObjArray[2] = "ABC";  
  25.         ObjArray[3] = "XYZ";  
  26.         ObjArray[4] = "DEF";  
  27.   
  28.         //Declaration of Int Array  
  29.         int[] objInt = new int[5];  
  30.         objInt[0] = 100;  
  31.         objInt[1] = 200;  
  32.         objInt[2] = 300;  
  33.         objInt[3] = 400;  
  34.         objInt[4] = 500;  
  35.   
  36.   
  37.         //Creation of array list  
  38.   
  39.         ArrayList ObjArraylist = new ArrayList();  
  40.         ObjArraylist.Add("Rajesh");  
  41.         ObjArraylist.Add(123);  
  42.   
  43.         //Creation of Dictionary  
  44.   
  45.         Dictionary < stringlong > phonebook = new Dictionary < stringlong > ();  
  46.         phonebook.Add("Rajesh", 415434543);  
  47.         phonebook["ABC"] = 415984588;  
  48.   
  49.         //Creation of Hashtable  
  50.   
  51.         Hashtable ht = new Hashtable();  
  52.         ht.Add("India""1");  
  53.         ht.Add("Bangalore""0");  
  54.   
  55.   
  56.         //Output of Int Array  
  57.         Console.WriteLine("Intiger array Output is");  
  58.         Console.WriteLine(objInt[v]);  
  59.   
  60.         //Output of string Array  
  61.         Console.WriteLine("String array Output is");  
  62.         Console.WriteLine(ObjArray[v]);  
  63.   
  64.         //Output of Int Array list  
  65.         Console.WriteLine("Arraylist Output is");  
  66.   
  67.         Console.WriteLine(ObjArraylist[v]);  
  68.   
  69.         //Output of Dictionary  
  70.   
  71.         Console.WriteLine("Dictionary Output is");  
  72.   
  73.         Console.ReadLine();  
  74.     }  
Output

Intiger array Output is: 100

String array Output is: Rajesh

Arraylist Output is: Rajesh

Dictionary Output is: ABC number is 415984588

 

Ebook Download
View all
Learn
View all