Add Row to two dimensional array - CSharp System

CSharp examples for System:Array Dimension

Description

Add Row to two dimensional array

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//w w  w  .  j  av a  2s . c  om

public class Main{
        public static T[,] AddRow<T>(this T[,] values, int pos, Func<int, T> newValue)
        {
            int width = values.GetLength(0);
            int height = values.GetLength(1);
            T[,] result = new T[width, height + 1];
            for (int j = 0; j < pos; j++)
                for (int i = 0; i < width; i++)
                    result[i, j] = values[i, j];

            for (int i = 0; i < width; i++)
                result[i, pos] = newValue(i);

            for (int j = pos; j < height; j++)
                for (int i = 0; i < width; i++)
                    result[i, j + 1] = values[i, j];
            return result;
        }
        public static T[,] AddRow<T>(this T[,] values, int pos, T[] newValues)
        {
            return AddRow(values, pos, i => newValues[i]);
        }
}

Related Tutorials