Replace value in an Array - CSharp System

CSharp examples for System:Array Operation

Description

Replace value in an Array

Demo Code


using System.Text;
using System.Linq;

public class Main{
        public static T[] Replace<T>(this T[] array, T oldValue, T newValue)
        {/*from  w w  w.  j a v a  2s.c  o m*/
            var retval = array
                .Select(arg => arg.Equals(oldValue) ? newValue : arg)
                .ToArray();

            return retval;
        }
        public static T[] ToArray<T>(this T[,] multiArray)
        {
            var lines = multiArray.CountLines();
            var columns = multiArray.CountColumns();
            var retval = new T[lines * columns];

            for (int i = 0; i < lines; i++)
                for (int j = 0; j < columns; j++)
                    retval[i * columns + j] = multiArray[i, j];

            return retval;
        }
        public static T[] ToArray<T>(this T[][] jaggedArray)
        {
            var elementsCount = jaggedArray.Sum(arg => arg.Length);
            var retval = new T[elementsCount];

            int index = 0;
            foreach (var t in jaggedArray)
                foreach (var t1 in t)
                    retval[index++] = t1;

            return retval;
        }
}

Related Tutorials