如何在 C# 中减少 int List 数组的值?

问题描述 投票:0回答:1

例如:我的列表包含:[5, 4, 3, 2, 1],我想每个值减1,新列表将是:[4, 3, 2, 1, 0],有可能,在 C# 中?

了解如何减少 List 中的值,如果可能的话,使用 List 函数或 LINQ。

c# arrays list linq arraylist
1个回答
0
投票
using System;

class Program
{
    static void Main()
    {enter code here
        int[] testArray = { 1, 2, 3 };`enter code here`
        Console.WriteLine(string.Join(", ", testArray));
        testArray = DecArray(testArray);
        Console.WriteLine(string.Join(", ", testArray));
    }

    static int[] DecArray(int[] a)
    {
        for (int i = 0; i < a.Length; i++)
        {
            a[i]--;
        }
        return a;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.