Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8127551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:34:16+00:00 2026-06-06T07:34:16+00:00

I am using an int array to hold a long list of integers. For

  • 0

I am using an int array to hold a long list of integers. For each element of this array I want to check if it is 1, if so do stuff relevant to 1 only, else if it is a 2, do other stuff relevant to 2, and so forth for each value stored in the array. I came up with the code below but it isn’t working as expected, is there something I am missing? What is happening is that only the first value of the array is being considered.

int[] variable1 = MyClass1.ArrayWorkings();
foreach (int i in variable1)
{ 
    if (variable1[i] == 1)
    {
        // arbitrary stuff
    }
    else if (variable1[i] ==2)
    {
        //arbitrary stuff
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:34:18+00:00Added an answer on June 6, 2026 at 7:34 am

    You’re trying to do something that doesn’t make sense. To see how it works, take a simple example, an array with values: 9, 4, 1.

    If you try to run your code on this sample array, you’ll get an error:

    foreach (int i in variable1)
    { 
        if (variable1[i] == 1)   // the item i is 9.  
                                 // But variable[i] means, get the value at position #9 in the array
                                 // Since there are only 3 items in the array, you get an Out Of Range Exception
        {
            // arbitrary stuff
        }
    {
    

    Instead, this is what you need:

    foreach (int i in variable1)  // i will be 9, then 4, then 1)
    { 
        if (i == 1)   
        {
            // arbitrary stuff
        }
        // ... etc
    }
    

    The alternative is to use a for loop, which would give you the indices 0, 1, and 2, like this:

    for (int i=0 ; i<=variable1.Length ; i++)   // i will be 0, 1, 2
                                                // variable[i] will be 9, 4, 1
    {
        if (variable1[i] == 1) 
        { 
            // stuff
        }
    
        // ... etc
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert an int to a byte[2] array using BCD. The int
I want to remove items of a Jagged array using indizes. int[] toRemove; (e.g,
I am using a List to hold some data obtained by calling Array.asList() method.
I'm using C++ and wondering if I can just send an entire int array
To assign specific value to 1D array I'm using LINQ like so: int[] nums
I built a symbol table using an array list filled with an object pairs
I am using an array to hold the results of an SQLite query, at
Is there any advantage of using int vs varbinary for storing bit masks in
What's the point of using : int in the enum declaration as following? public
I am printing a chunk from inputstream by using int skip = in.skip(1024); //skip

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.