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'
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
- Basics of OOPs (Object Oriented Programming System)
- Straightforward points about Overriding
- C# Method Overloading- in few simple steps with example
- Talking about C# Interface: in few simple straightforward steps
- C# code Abstract method- In few simple straightforard steps
- Select start up project in just 3 steps in visual studio
- Starting with array variable
- whether to press F5 or ctrl+F5 to execute my program.
- How to write c sharp program in 8 simple steps using Visual Studio
- Catching the value of any Kind of Dialog Boxes
- Line Number along with Code in VS2008 / VS2010 / VS2012
No comments:
Post a Comment