Click here to Skip to main content
15,793,452 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
See more:
Dear Sir,

how to solve Index was outside the bounds of the array in c# language.

note this is my Example:

string[] Address1= {"CTS IT PARK","2nd Cross", "#87 Tambaram to Velachery Main Road", "Chennai- 73"};

this Array 4 values stored.

string[] Address2={"TCS IT PARK","Bangalore"};

this Array 2 values stored.

now i am using for - loop

for (i = 0, j = 0; i < address1.Length - 1 || j < address2.Length - 1; i++, j++)
WriteLine(address1[i],address2[j]);//Error. What can i do here.

but error in my loop Index was outside the bounds of the array. in c#

Give me solution how to solve this any alternate way then give me solution

By mohan(kainspire)
Updated 2-May-22 3:50am
Sergey Alexandrovich Kryukov 24-Dec-11 0:34am    
Did you delete you question when I already submitted an answer. This one:
If you did that, it's too bad. I spend good amount of time to help you, have written a big function...

So, what's so wonderful? You just have two array with different lengths. You index range goes outside one of the arrays. OK, I feel my previous answer could help. You previous question was deleted, which is too bad (who did it?!!).

You code simply does not have much sense.

However… this task makes some sense if made generalized. First, let's make an array element a generic parameters, because the problem does not really depend on it. Let's make the number of arrays arbitrary, number of elements in each array arbitrary and abstract our the conditions. Also, let's make a number of conditions arbitrary.

That can make some sense, at least remotely; and of course the ad-hoc problem would not have much sense. What, with hard-coded array ranges, etc.? Come on…

This set of input data will generate more then one method checking up the condition, because — how to define what is the check? I'll show just one of such predicate method, the one which validates that all conditions for all array elements are held true:

static bool AllConditionsValidForAllArrayElements<T>(
                T[][] arrays,
                T, bool>[] conditions)
    foreach (T[] array in arrays) {
        int index = 0;
        foreach(T element in array)
            foreach (System.Func<int, T, bool> condition in conditions) {
                if (!condition(index, element))
                    return false;
            } //loop in conditions
    } // loop in arrays
    return true;
} //AllConditionsValidForAllArrayElements

[EDIT] Using the same principle, one could build different predicates on this data set, such as "at least one array element satisfies at least one condition", "at least one array element satisfies all conditions", "all elements of at least one array satisfy at least on condition", "all elements of at least one array satisfy all conditions", "at least one condition is satisfied by all array elements", etc. The one who can list all of them is at least a good student. :-)

Only one problem here: the simplest analysis shows that the nested cycles is better in performance than any thinkable ways of merging them in one cycle, so what? I've warned about the fantasy in my comment to the first version of this question…

Share this answer
Rajesh Anuhya 24-Dec-11 0:40am    
Good Answer have my +5
Sergey Alexandrovich Kryukov 24-Dec-11 0:53am    
Thank you, Rajesh,
Amir Mahfoozi 24-Dec-11 1:13am    
Sergey Alexandrovich Kryukov 24-Dec-11 1:18am    
Thank you, Amir. I wonder if you voted before or after the last fixes, after [EDIT]? :-)
Amir Mahfoozi 24-Dec-11 1:22am    
It was for nice effort to describe the situation for him. It worth five even before the edited part ;) .
Here address1 array has more item then address2 so you have index out of range problem

try below code.

for (i = 0, j = 0; i < address1.Length - 1 || j < address2.Length - 1; i++, j++)
if(j < address2.length)
// or what ever you want to display.


Please let me know is this helpful to you?
Share this answer
Hi, your problem is pretty simple! You need an AND (&&) instead of OR (||)!! While the one array exceeds its limits and gives a False in the query the other array is still within its limits and returns a True at the query! Therefore your for-loop will continue and throw an error ;)

Your for-loop should be:

for (i = 0, j = 0; i < address1.Length - 1 && j < address2.Length - 1; i++, j++)
Share this answer
Richard Deeming 3-May-22 12:17pm    
Note the date: you've posted a new solution to a solved question from 2011. Although your answer is correct, and doesn't seem to have been covered by the previous solutions, some users will automatically down-vote your solution due to the age of the question.

Given the numbering of the solutions, I suspect this may have been dragged back into the "active" list by a spam solution which has since been deleted.

Also, your solution has replicated one problem from the question: the last element of both arrays will be missed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900