Saturday 2 May 2015

How to use C# ArrayList Class

ArrayList:-

Array lists are not strongly type collection. It will store values of different datatypes or same datatype. Array list size will increase or decrease dynamically it can take any size of values from any data type. These Array lists will be accessible with “System.Collections” namespace

ArrayList is one of the most flexible data structure from CSharp Collections. ArrayList contains a simple list of values. ArrayList implements the IList interface using an array and very easily we can add , insert , delete , view etc. It is very flexible because we can add without any size information , that is it will grow dynamically and also shrink .

Operations On ArrayList:-


 Add :
          Add method we use to append Items at the end of an ArrayList
  Insert :
          Insert method  we use Add Item in a specified position in an ArrayList
  Remove :
          Remove method we use to Item from ArrayList by specifying Item's name
  RemoveAt: 
          RemoveAt method we use to remove an item from a specified position
  Sort :
          To Sort Items in an ArrayList

Note :- All operation are operate on  the Object of ArrayList 

Source code Example



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Collections;

Namespace ConsoleDemoApp
{
    class Test
    {
        /* program to implement ArrayList in c# */

        public static void Main(string[] args)
        {
            int inputNum=0;

          
            Console.Write("\nEnter number of students to be add: ");
            inputNum= int.Parse(Console.ReadLine());

            ArrList arList = new ArrList();
            arList.ArrListOperations(inputNum);
            Console.ReadKey();
        }
    }

  
  

    Public class ArrList
    {
        public void ArrListOperations(int inputNum)
        {
          
            ArrayList ItemList = new ArrayList();
            for (int i=1; i<=inputNum; i++)
            {
            ItemList.Add("Item"+i);
            }

            // print
            Console.Write("\nEntered Items are: \n");
            for (int j = 0; j <= ItemList.Count-1; j++)
            {
               Console.Write(ItemList[j].ToString());
               Console.Write("\n");
            }

            //insert an item
            ItemList.Insert(3, "Item6");
            Console.Write("\nAfter inserting an item Item6 at 3 index: \n");
            for (int j = 0; j <= ItemList.Count - 1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }

            //sort itemms in an arraylist
            ItemList.Sort();
            Console.Write("\nAfter shorting ArrryList: \n");
            for (int j = 0; j <= ItemList.Count - 1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }


            //remove an item
            ItemList.Remove("Item1");
            Console.Write("\nAfter Removing item Item1: \n");
            for (int j = 0; j <= ItemList.Count - 1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }



            //remove item from a specified index
            ItemList.RemoveAt(3);
            // final ArrayList
            Console.Write("\n After Removing item at index 3, items are: \n");
            for (int j = 0; j <= ItemList.Count-1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }
        }
    }
}


Output:-


What is a Interface in C# .Net

An interface looks like a class, but has no implementation.The reason interfaces only provide declarations is because they are inherited by classes and structs, which must provide an implementation for each interface member declared.

Properties Of Interface:-

  1. Supports multiple inheritance(Single Class can implement multiple interfaces).
  2. Can not Contains data members.
  3. By Default interface members is public (We Can not set any access modifier into interface members).
  4.  Interface can not contain constructors.



Interface IMyInterface
{
    void MethodToImplement();
}

Interface is a type which contains only the signatures of methods, delegates or events, it has no implementation. Implementation of the methods is done by the class that which implements the interface.
Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing ‘Idisposable’ interface that means it has a functionality to release unmanaged resources.

If a class implements a interface then it has to provide implementation to all its methods.



Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.IO;
Using System.Globalization;
Using System.Data;

Namespace ConsoleDemoApp
{
    Class Test
    {
        /* program to implement interface in c# */

        Public static void Main(string[] args)
        {
            double dueAmt, ServiceCharge, Receipt;

           
            Console.Write("\nEnter Due Amount : ");
            dueAmt = Convert.ToDouble(Console.ReadLine());
            Console.Write("\nEnter Service Charge: ");
            ServiceCharge = Convert.ToDouble(Console.ReadLine());

            Payment pay = new Payment();
            // make payment
            pay.PayDueAmt(dueAmt, ServiceCharge);
            //print payment history
            Receipt = pay.getTotalPaidAmt();
            Console.Write("\nTransaction is successful");
            Console.Write("\nTotal paid amount is : {0}", Receipt);
            Console.ReadKey();
        }
    }

    // Interface
    public interface IPayment
    {
        // member of interface
        Void PayDueAmt(Double dueAmt, Double serviceCharge);
        Double getTotalPaidAmt();
      
    }

    // class Payment implements the above interface
    Public class Payment : IPayment
    {
        Double paidAmt, ServiceCharge, Total;
        // method 1
        Public void PayDueAmt(Double dueAmt, Double serviceCharge)
        {
            this.paidAmt = dueAmt;
            this.ServiceCharge=serviceCharge;
        }
        // method 2
        Public Double getTotalPaidAmt()
        {
            this.Total = this.paidAmt + this.ServiceCharge;
            Return this.Total;
        }
    }
}


  Output














Summary
You now understand what interfaces are. You can implement an interface and use it in a class. Interfaces may also be inherited by otherinterface. Any class or struct that inherits an interface must also implement all members in the entire interface inheritance chain.