c# collection part-1

Collection in c#.

  1. Collections are commonly similar to Array. It provides a much good way to work with a group of objects.
  2. We don't have to define the size of the collection as we had done in Array.
  3. we can add element and remove an element at any point in the collection.

1. Array List : 

The ArrayList collection is similar to the Arrays data type in C#. The biggest difference is the dynamic nature of the array list collection. which means we don't have to set the size of the ArrayList before.
2. Stack : 

The stack is a special case collection which represents a last in first out (LIFO) concept
3. Queues.

The Queue is a special case collection which represents a first in first out concept.

4. HashTable :

A hash table is a special collection that is used to store key-value items

What is ArrayList in C#?


The ArrayList collection is similar to the Arrays data type in C#. The biggest difference is the dynamic nature of the array list collection.

For arrays, you need to define the number of elements that the array can hold at the time of array declaration. But in the case of the Array List collection, this does not need to be done before. 

Elements can be added or removed from the Array List collection at any point in time. Let's look at the operations available for the array list collection in more detail.

Declaration of an  Array List

ArrayList a1 = new ArrayList()

Adding elements to an arrayList

  • a1.add(1) – This will add an Integer value to the collection
  • a1.add("Example") – This will add a String value to the collection
  • a1.add(true) – This will add a Boolean value to the collection
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoApplication
{
 class Program
 {
  static void Main(string[] args)
  {
   ArrayList a1 = new ArrayList();
   a1.Add(1);
   a1.Add("Example");
   a1.Add(true);
   
   Console.WriteLine(a1.Count);
   Console.WriteLine(a1.Contains(2));
   Console.WriteLine(a1[1]);
   a1.RemoveAt(1);
   Console.WriteLine(a1[1]);
   Console.ReadKey();
  }
 }
}

Count

This method is used to get the number of items in the ArrayList collection. 
ArrayList.Count() – This method will return the number of elements that the array list contains.

Contains

This method is used to see if an element is present in the ArrayList collection. 
ArrayList.Contains(element) – This method will return true if the element is present in the list, else it will return false.

RemoveAt

This method is used to remove an element at a specific position in the ArrayList collection. 
ArrayList.RemoveAt(index) – This method will remove an element from a specific position of the Array List.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoApplication
{
 class Program
 {
  static void Main(string[] args)
  {
   ArrayList al = new ArrayList();
   al.Add(1);
   al.Add("Example");
   al.Add(true);
   
   Console.WriteLine(a1.Count);
   Console.WriteLine(a1.Contains(true));
   Console.WriteLine(a1[1]);
   a1.RemoveAt(2);
   Console.WriteLine(a1[1]);
   Console.ReadKey();
  }
 }
}

Comments

Popular posts from this blog

Asp.Net Validation

Fundamental of OOPS From Scratch to End