Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,705 questions

55,464 answers

573 users

How to create and set values to a 3d array in C#

1 Answer

0 votes
using System;

class Program
{
    static void InitializeArray(int[,,] array, int x, int y, int z) {
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                for (int k = 0; k < z; k++) {
                    array[i, j, k] = i + j + k; // initialization
                }
            }
        }
    }

    static void PrintArray(int[,,] array, int x, int y, int z)
    {
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                for (int k = 0; k < z; k++) {
                    Console.Write(array[i, j, k] + " ");
                }
                Console.WriteLine();
            }
        }
    }

    static void Main()
    {
        int x = 2, y = 3, z = 4;
        int[,,] array = new int[x, y, z]; // Create a 2x3x4 array

        InitializeArray(array, x, y, z);
        PrintArray(array, x, y, z);
    }
}



/*
run:

0 1 2 3 
1 2 3 4 
2 3 4 5 
1 2 3 4 
2 3 4 5 
3 4 5 6  

*/

 



answered Apr 21, 2025 by avibootz
...