9.6.13

Starting with array variable

Intro to Array variable

Array is just like another variable in C#, which holds the same type of value under the same name.
E.g. nameOfStudents (string type array), marksOfSubject (integer/Number type array), listOfTemperature (double/float type array).


So here is a quick and short work around on Arrays...

Lets see them in action first, what kind of value an Array object store and more important how it does.

In the programming way:

 string[] crateOfColdDrinks= new string[4];//declaration of array
                crateOfColdDrinks[0] = "coke"; //initialization of array
                crateOfColdDrinks[1] = "thumbsup"; //initialization of array

                crateOfColdDrinks[2] = "fantas"; //initialization of array

                crateOfColdDrinks[3] = "maza"; //initialization of array

                //crateOfColdDrinks[3] = "pepsi"; //erroneous initialization of array

                int i;

                for (i = 0; i < 4; i++)
                {
                    Console.WriteLine("Name of Cold Drink : crateOfColdDrinks[" + i + "]: " 
+ crateOfColdDrinks[i]);
                }

when you are dealing with an array you must take care of its range value.
Range value means, go to line 1 of the code block above, there we have given value 4 in between the brackets or space for name of 4 cold drinks.

Either you might assigned values less than this '4' or greater (also known as index value/or range value). See the commented out erroneous assignment  of pepsi in above program.


You would see an error, known as exception error like this 


Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at programname.Program.Main(String[]args)in 'path or address of program directory' 

It just tell us in very simple language that Index was outside the bounds of the array.

To resolve this smartly C# has given us just another powerful feature that is known as "Exception Handling." 
Post related to CSharp programming

No comments:

Post a Comment

Search This Blog

Let make your friend aware about this post