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 6836229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:19:52+00:00 2026-05-26T23:19:52+00:00

Here’s an extension method each which can be used to apply an Action<int, int,

  • 0

Here’s an extension method each which can be used to apply an Action<int, int, T> to every element and it’s corresponding indices of a two-dimensional array:

static public void each<T>(this T[,] a, Action<int, int, T> proc)
{
    for (int i = 0; i < a.GetLength(0); i++)
        for (int j = 0; j < a.GetLength(1); j++)
            proc(i, j, a[i, j]);
}

Example usage:

var arr = new int[3, 3];

arr.each((x, y, val) => arr[x, y] = x + y);

arr.each((x, y, val) => Console.WriteLine("{0} {1} {2}", x, y, val));

Is it possible to write a version of each which can work on arrays of any rank?

  • 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-05-26T23:19:52+00:00Added an answer on May 26, 2026 at 11:19 pm

    There’s no way to specify a generic with an arbitrary number of parameters, so you would have to pass your procedure an array of indices. The easiest way to do this is probably with recursion so here’s a recursive way to do it. Since it uses an optional parameter it requires C# 4.0 or higher.

    static class Arrays
    {
        public static void Each<T>(this Array a,
                                   Action<int[], T> proc,
                                   int[] startAt = null)
        {
            int rank = startAt == null ? 0 : startAt.Length;
            int[] indices = new int[rank + 1];
            if (rank > 0)
                startAt.CopyTo(indices, 0);
            for (int i = a.GetLowerBound(rank); i <= a.GetUpperBound(rank); i++)
            {
                indices[rank] = i;
                if (rank == a.Rank - 1)
                    proc(indices, (T)a.GetValue(indices));
                else
                    Each(a, proc, indices);
            }
        }
    }
    

    You would call it like this:

    var array = new int[1, 2, 3, 4, 5];
    array.Each<int>((indices, data) => array.SetValue(indices.Sum(), indices));
    array.Each<int>((indices, data) =>
          Console.WriteLine(string.Join(", ", indices) + ": " + data));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code, which takes two version identifiers in the form 1, 5,
I used javascript for loading a picture on my website depending on which small
Here is my problem : I have a post controller with the action create.
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is my code sample, let me know if it can be further improved?
Here a simple question : What do you think of code which use try
Here's the situation, i want to have a user that can enter time on
Here is an example: I have a file 1.js, which has some functions. I
Here is a script I can't get to work correctly. It's purpose is to
Here's a basic regex technique that I've never managed to remember. Let's say I'm

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.