Saturday, 2 December 2017

C# Program to Demonstrate IndexOutOfRange Exception


Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace differnce
{
    class arrayoutofindex
    {
        public void calculatedifference()
        {
            int difference=0;
            int [] number= new int[5] {1,2,3,4,5};
            try
            {
                for (int init =1; init <=5; init++)
                {
                    difference= difference -  number[init];
                }
                Console.WriteLine("The difference of the array is:" + difference);
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
    class classmain
    {
        static void Main(string [] args)
    {
        arrayoutofindex obj = new arrayoutofindex();
        obj.calculatedifference();
        Console.ReadLine();
    }
}
}


Output:

Index was outside the bounds of the array



More C# Programs:


















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...