Jun
12
2013

C# Array Best Practice: C# Best Practice of Array Iteration

C# best practices is always a topic that is mostly searched on the internet. In this article we are going to see a c# array best practice of iteration. Before start lets understand what is an array in c#?

What are arrays in C#.Net?

We all know that arrays are the collection of variables(elements) of same type which gets arranged in indexing format. The elements of array then can be accessed using index number. Array is different from normal scalar variable. A scalar variable can hold only single element while array can hold multiple elements of same data types. A graphical representaion of C# Arrays is given below. Arrays are of reference types and arrays in c#.net are zera based.

C# Array Best Practice - Array Representation
C# Array Representation in C#.Net

You might be aware how to use Arrays in C#. But in this article we are going to learn the efficient way of array iteration in c#. Why you should read this article? : Each and every component of C#.Net should be used in efficient way. Because it improves application’s performance. Most of the developers in their early phase of the career know how to use arrays in C#.net however they less aware of technique to use arrays in efficient way. Most of the time developers don’t think about the performance of the application. Hence if you are a programmer you must be aware of best practices in c#.net.

Array can also be serialized and Deserialized in c#.net.

Efficient way of array iteration in C#.net or C# Array Best Practice

In this c# tutorial let’s take a look at c# best practices of Array iteration in c#.net. Do you know why we should always follow C# Array Best Practice.

Why arrays should be used in efficient way?

Simple answer to this is to improve the speed/performance of the application. I hope by the end of this article you would be able to use array in efficient way and would realize a significant improvement in performance of your application.

Before we continue, can you answer to a simple question. Do you know about IndexOutOfRangeException and when it occurs? The answer is very simple and I know you guys know this. Yes! The exception occurs when you try to access an index value of an array which is more than the actual length of array.

Now, see below code for looping array in c# with example

   try
   {
       int[] simpleArray = new int[10];
       for (inti = 0; i < 12; i++)
       {
           simpleArray[i] = i;
           Console.Write(” “ + simpleArray[i]);
       }
   }
   catch (IndexOutOfRangeExceptionex)
   {
       Console.WriteLine(“IndexOutOfRangeException occurs. Check the loop.”);
   }

As you can see in above code snippet, the length of ‘simpleArray’ is declared as 10 and while iterating in ‘for’ loop the iteration count is set to 12.

We know that c# supports ‘0’ based index collections. Hence when ‘for’ loop reaches to index value 10 it would throw an IndexOutOfRangeException.

So is this the efficient way of handling array looping? No, most of the time we may not be sure the length of the array. The best way to iterate thru an array collection is to know the maximum length of an array, so that the iteration never goes beyond that index value. This maximum value is also called as upper bound of an array. Putting a hardcoded value as an upper bound in array iteration like above code snippet is not a good idea.

There are two ways to efficiently handle the array iteration.

A. Use upper bound of an array as length while iterating

The first way is to get the length of an array as an upper bound while iterating thru array. For example look at below code –

   int[] simpleArray = new int[10];
   for (inti = 0; i < simpleArray.Length; i++)
   {
       simpleArray[i] = i;
       Console.Write(” “ + simpleArray[i]);
   }

As shown in above code, instead of writing hardcoded value as an upper bound of an array looping, ‘array.Length’ is used. So in this case, the loop would continue to iterate to i=9 and then terminates itself without throwing an IndexOutOfRangeException.

B. Use ‘foreach’ instead of ‘for’

The second best way is to prefer ‘foreach’ loop over a ‘for’ loop. Below example shows how to use ‘foreach’ loop for an array iteration.

   int[] simpleArray = new int[10];
   int j = 0;
     foreach(int i in simpleArray)
   {
       simpleArray[i] = j++;
       Console.Write(” “ + simpleArray[i]);
   }

Above two are efficient ways of array iteration in c#, because compiler will never throw an IndexOutOfRangeException.

I hope this article helped you to understand how to use C# arrays in efficient ways. This c# best practice would definately improve your application performacne.

Please feel free to share the article on your favourite social media using below “Sharing is Caring” widget.

More interesting articles –

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

3 Comments+ Add Comment

Leave a comment