Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# get column of 2d array

public class CustomArray<T>
{
    public T[] GetColumn(T[,] matrix, int columnNumber)
    {
        return Enumerable.Range(0, matrix.GetLength(0))
                .Select(x => matrix[x, columnNumber])
                .ToArray();
    }

    public T[] GetRow(T[,] matrix, int rowNumber)
    {
        return Enumerable.Range(0, matrix.GetLength(1))
                .Select(x => matrix[rowNumber, x])
                .ToArray();
    }
}
Comment

2d array rows and columns in c#

using System;
using System.Linq;
using System.Runtime.InteropServices;

namespace ConsoleApplication4
{
    public static class Program
    {
        private static void Main()
        {
			/*
				C# supports multidimensional arrays up to 32 dimensions.
    			The multidimensional array can be declared by adding commas in the
    			square brackets.
    			For example, [,] declares two-dimensional array,
    			[, ,] declares three-dimensional array,
    			[, , ,] declares four-dimensional array, and so on.
    			So, in a multidimensional array, number of commas = Number of Dimensions.
			*/

			// For instance:
			int[,] arr2d; // two-dimensional array
			int[, ,] arr3d; // three-dimensional array
			int[, , ,] arr4d ; // four-dimensional array

          // declare and instantiate a 2d array with 4 rows and 5 columns
          	var array = new int[4,5](); 
          	/* The [4, 5] defines the number of rows and columns.
          	   The first rank (4) denotes the number of rows, and the second rank (5)
               defines number of columns.
               The code below instantiate and illustrate the 2d array divide into rows and columns.
          	*/
          
          	var array = new [,] // or var array = new int[4,5]
            {
              // col0,col1,col2,col3,col4
                {0.1, 0.2, 0.3, 0.4, 0.5}, // row0
                {1.1, 1.2, 1.3, 1.4, 1.5}, // row1
                {2.1, 2.2, 2.3, 2.4, 2.5}, // row2
                {3.1, 3.2, 3.3, 3.4, 3.5}, // row3
            };
			
          	// Get the element 0.2 (that is row0, col1)
          	array[0, 1]; //returns 0.2
          	// Get the element 2.3 (that is row2, col2)
          	array[2, 2]; //returns 2.3          

			//array[4, 1]; //throws run-time error as there is no 4th row
          	//array[2, 5]; //throws run-time error as there is no 5th column
          // get the third row
            var row = array.GetRow(2);

            // This prints 2.1, 2.2, 2.3, 2.4, 2.5
            Console.WriteLine(string.Join(", ", row.Select(element => element.ToString())));
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: cread 2-dimensional array in c# 
Csharp :: c# convert securestring to string 
Csharp :: Non-Serialized Fields in unity inspector 
Csharp :: c# file to byte array 
Csharp :: c# temporary files 
Csharp :: how prevent user remove file linux 
Csharp :: Palindromic substrings 
Csharp :: check if element in hashset c# 
Csharp :: unity script template folder 
Csharp :: Severity Code Description Project File Line Suppression State Error MSB3021 
Csharp :: rigidbody.velocity.magnitude 
Csharp :: unity normalize 
Csharp :: c# list object 
Csharp :: convert list of string to dictionary 
Csharp :: ef core add OnModelCreating foreign key 
Csharp :: C# Convert xml to datatable 
Csharp :: save binary data to file c# 
Csharp :: ex: c# last item in array 
Csharp :: array to object c# 
Csharp :: static property in c# 
Csharp :: unity trygetcomponent 
Csharp :: calculate textbox value c# 
Csharp :: Moq Unittest with ILogger 
Csharp :: c# null accessor 
Csharp :: singleton pattern c# stack overflow 
Csharp :: constant interpolated string 
Csharp :: Task timed out after 10.02 seconds 
Csharp :: How to determine whether Task.Run is completed within a loop in c# 
Csharp :: how to navigate between page in wpf 
Csharp :: to string c# fields 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =