Add Column two dimensional array - CSharp System

CSharp examples for System:Array Dimension

Description

Add Column two dimensional array

Demo Code


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

public class Main{
        public static T[,] AddColumn<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 + 1, height];
            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < pos; i++)
                    result[i, j] = values[i, j];
                result[pos, j] = newValue(j);
                for (int i = pos; i < width; i++)
                    result[i + 1, j] = values[i, j];
            }
            return result;
        }
        public static T[,] AddColumn<T>(this T[,] values, int pos, T[] newValues)
        {
            return AddColumn(values, pos, i => newValues[i]);
        }
}

Related Tutorials