Batch operates all the elements in an array

Problem:

You have an array consists of a series of numbers, and you want to have all the numbers subtract 1 by themselves.

Solution:

Use <object>.Select() extension method provided by System.Linq.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Learning
{
    class ArrayTesting
    {
        static void Main()
        {
            int[] a = new int[] { 1, 2, 3, 4, 5 };
            // Have each element in the array subtract 1.
            a = a.Select(i => i - 1).ToArray();

            Console.WriteLine(string.Join(", ", a));
            Console.ReadKey();
        }
    }
}

Output:

batch operates the elements of an array

Add comment

Loading