14,734,830 members
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:

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 22-Oct-17 10:26am
v7

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 2

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++)
{
{
}
else
{
// or what ever you want to display.
}

}```

## 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:

```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

Thank you, Rajesh,
--SA
Amir Mahfoozi 24-Dec-11 1:13am

+5

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 ;) .
[no name] 24-Dec-11 1:22am

My +5 too
--MD
Sergey Alexandrovich Kryukov 24-Dec-11 16:51pm

Thank you, Maulik,
--SA
Monjurul Habib 24-Dec-11 2:42am

5!
Sergey Alexandrovich Kryukov 24-Dec-11 16:52pm

Thank you, Monjurul.
--SA
thatraja 24-Dec-11 2:48am

5!
Sergey Alexandrovich Kryukov 24-Dec-11 16:52pm

Thank you, Raja.
--SA
Little Soldier 3-Apr-14 4:47am

how to use it?
AllConditionsValidForAllArrayElements

Compare the length, apply some logic...
—SA