15,793,452 members
See more:
Dear Sir,

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

note this is my Example:

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

this Array 4 values stored.

2.
```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++)
{
}```

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)
Posted
Updated 2-May-22 3:50am
v7
Sergey Alexandrovich Kryukov 24-Dec-11 0:34am
Did you delete you question when I already submitted an answer. This one:
http://www.codeproject.com/Questions/305052/How-to-write-code-for-checking-2-condition-in-Fore
If you did that, it's too bad. I spend good amount of time to help you, have written a big function...
--SA

## Solution 1

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:

C#
```static bool AllConditionsValidForAllArrayElements<T>(
T[][] arrays,
System.Func<int,
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;
++index;
} //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…

—SA

v2
Rajesh Anuhya 24-Dec-11 0:40am
Sergey Alexandrovich Kryukov 24-Dec-11 0:53am
Thank you, Rajesh,
--SA
Amir Mahfoozi 24-Dec-11 1:13am
+5
Sergey Alexandrovich Kryukov 24-Dec-11 1:18am
Thank you, Amir. I wonder if you voted before or after the last fixes, after [EDIT]? :-)
--SA
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 ;) .

## Solution 2

Here address1 array has more item then address2 so you have index out of range problem

try below code.

C#
```for (i = 0, j = 0; i < address1.Length - 1 || j < address2.Length - 1; i++, j++)
{
{
}
else
{
// or what ever you want to display.
}

}```

## Solution 5

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 ;)

`for (i = 0, j = 0; i < address1.Length - 1 && j < address2.Length - 1; i++, j++)`

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.