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

  • Home
  • SEARCH
  • 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 8526053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:09:35+00:00 2026-06-11T08:09:35+00:00

I have some code that works with a two-dimensional grid (array) and I ended

  • 0

I have some code that works with a two-dimensional grid (array) and I ended up with a lot of routines that loop through each item in the grid.

But I thought it would be nice if I could do this LINQ-style using extension methods.

Here’s what I tried so far:

private static void ForEach(this MyType[,] grid, Action<MyType> action)
{
    for (int row = 0; row < grid.GetUpperBound(0); row++)
        for (int col = 0; col < grid.GetUpperBound(1); col++)
            action(grid[row, col]);
}

1. Intellisense doesn’t seem to recognize this extension method on an object of type MyType[3,3]. Am I missing something about writing an extension method for a two-dimensional array?

2. What is the recommended way to implement such a pattern that allows me to conditionally set the grid value? Using the code above, setting the passed value would not affect the original array.

EDIT:

Okay, after originally implementing this extension method within the same class it was called from, I stupidly left it declared as private when I moved it to an external class. I guess it won’t work as a private method in the same class and it obviously won’t work as a private method in an external class.

Would still love to see an answer to number 2 though.

  • 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-11T08:09:37+00:00Added an answer on June 11, 2026 at 8:09 am

    For your second question, an Action will not work because it does not return a value, so it could not directly update the array. What you may want is a Func<MyType,MyType> that will take in the original value, do some processing, and return a new value that your ForEach will then plop back into the array. If you need to know the row and column then make it a Func<MyType, int, int>.

    As a side note, I would not call it ForEach since you are modifying the underlying collection (which ForEach cannot do in a normal collection). I would call it UpdateAll or something like that.

    So you end up with something like:

    public static void UpdateAll(this MyType[,] grid, Func<MyType,MyType> action)
    {
        for (int row = grid.GetLowerBound(0); row <= grid.GetUpperBound(0); row++)
            for (int col = grid.GetLowerBound(1); col <= grid.GetUpperBound(1); col++)
                grid[row, col] = action(grid[row, col]);
    }
    

    Note that it uses GetLowerBound and uses <= instead of < on the upper bound check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that works each time onmouseclick and continuously onmousemove when I
I have some code that works fine in FF but not in IE. I
I have some JavaScript code that works in IE containing the following: myElement.innerText =
I have some login code that works well for Firefox and other browsers, but
I have some very basic semaphore code that works great on Linux, but cannot
i have some javascript roll over code that works fine in firefox but when
I have some toy code for a tooltip. It works okay, except that when
I have some Scala code that does something nifty with two different versions of
I have some PHP code that should cause and catch two exceptions: try{ @$this->connector->connect();
I have written some code that works great, but I don't understand why it

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.