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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:42:32+00:00 2026-05-17T02:42:32+00:00

I’m looking for a way to set every value in a multidimensional array to

  • 0

I’m looking for a way to set every value in a multidimensional array to a single value. The problem is that the number of dimensions is unknown at compile-time – it could be one-dimensional, it could be 4-dimensional. Since foreach doesn’t let you set values, what is one way that I could achieve this goal? Thanks much.

  • 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-17T02:42:32+00:00Added an answer on May 17, 2026 at 2:42 am

    While this problem appears simple on the surface, it’s actually more complicated than it looks. However, by recognizing that visiting every position in a multidimensional (or even jagged) array is a Cartesian product operation on the set of indexes of the array – we can simplify the solution … and ultimately write a more elegant solution.

    We’re going to leverage Eric Lippert’s LINQ Cartesian Product implementation to do the heavy lifting. You can read more about how that works on his blog if you like.

    While this implementation is specific to visiting the cells of a multidimensional array – it should be relatively easy to see how to extend it to visit a jagged array as well.

    public static class EnumerableExt
    {
        // Eric Lippert's Cartesian Product operator...
        public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(
              this IEnumerable<IEnumerable<T>> sequences)
        {
            IEnumerable<IEnumerable<T>> emptyProduct = 
                       new[] { Enumerable.Empty<T>() };
            return sequences.Aggregate(
              emptyProduct,
              (accumulator, sequence) =>
                from accseq in accumulator
                from item in sequence
                select accseq.Concat(new[] { item }));
        }
    }
    
    class MDFill
    {
        public static void Main()
        {
            // create an arbitrary multidimensional array
            Array mdArray = new int[2,3,4,5];
    
            // create a sequences of sequences representing all of the possible
            // index positions of each dimension within the MD-array
            var dimensionBounds = 
                Enumerable.Range(0, mdArray.Rank)
                   .Select(x => Enumerable.Range(mdArray.GetLowerBound(x),
                          mdArray.GetUpperBound(x) - mdArray.GetLowerBound(x)+1));
    
            // use the cartesian product to visit every permutation of indexes
            // in the MD array and set each position to a specific value...
            int someValue = 100;
            foreach( var indexSet in dimensionBounds.CartesianProduct() )
            {
                mdArray.SetValue( someValue, indexSet.ToArray() );
            }
        }
    }
    

    It’s now trivial to factor this code out into a reusable method that can be used for either jagged or multidimensional arrays … or any data structure that can be viewed as a rectangular array.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I am currently running into a problem where an element is coming back from

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.