Click here to Skip to main content
14,734,606 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
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 22-Oct-17 10:26am
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…

Rajesh Anuhya 24-Dec-11 0:40am
Good Answer have my +5
Thank you, Rajesh,
Amir Mahfoozi 24-Dec-11 1:13am
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 ;) .
[no name] 24-Dec-11 1:22am
My +5 too
Sergey Alexandrovich Kryukov 24-Dec-11 16:51pm
Thank you, Maulik,
Monjurul Habib 24-Dec-11 2:42am
Sergey Alexandrovich Kryukov 24-Dec-11 16:52pm
Thank you, Monjurul.
thatraja 24-Dec-11 2:48am
Sergey Alexandrovich Kryukov 24-Dec-11 16:52pm
Thank you, Raja.
Little Soldier 3-Apr-14 4:47am
how to use it?
Compare the length, apply some logic...
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?

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