<small id='DHvKz'></small><noframes id='DHvKz'>

<i id='DHvKz'><tr id='DHvKz'><dt id='DHvKz'><q id='DHvKz'><span id='DHvKz'><b id='DHvKz'><form id='DHvKz'><ins id='DHvKz'></ins><ul id='DHvKz'></ul><sub id='DHvKz'></sub></form><legend id='DHvKz'></legend><bdo id='DHvKz'><pre id='DHvKz'><center id='DHvKz'></center></pre></bdo></b><th id='DHvKz'></th></span></q></dt></tr></i><div id='DHvKz'><tfoot id='DHvKz'></tfoot><dl id='DHvKz'><fieldset id='DHvKz'></fieldset></dl></div>

      <tfoot id='DHvKz'></tfoot>
      • <bdo id='DHvKz'></bdo><ul id='DHvKz'></ul>
    1. <legend id='DHvKz'><style id='DHvKz'><dir id='DHvKz'><q id='DHvKz'></q></dir></style></legend>

    2. C#:更改数组中每个项目的值

      C#: Altering values for every item in an array(C#:更改数组中每个项目的值)
      <i id='N5elC'><tr id='N5elC'><dt id='N5elC'><q id='N5elC'><span id='N5elC'><b id='N5elC'><form id='N5elC'><ins id='N5elC'></ins><ul id='N5elC'></ul><sub id='N5elC'></sub></form><legend id='N5elC'></legend><bdo id='N5elC'><pre id='N5elC'><center id='N5elC'></center></pre></bdo></b><th id='N5elC'></th></span></q></dt></tr></i><div id='N5elC'><tfoot id='N5elC'></tfoot><dl id='N5elC'><fieldset id='N5elC'></fieldset></dl></div>
        <tbody id='N5elC'></tbody>
        <bdo id='N5elC'></bdo><ul id='N5elC'></ul>

      • <legend id='N5elC'><style id='N5elC'><dir id='N5elC'><q id='N5elC'></q></dir></style></legend>
            • <small id='N5elC'></small><noframes id='N5elC'>

                <tfoot id='N5elC'></tfoot>
                本文介绍了C#:更改数组中每个项目的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想知道是否有内置的 .NET 功能可以根据提供的委托的结果更改数组中的每个值.例如,如果我有一个数组 {1,2,3} 和一个返回每个值平方的委托,我希望能够运行一个采用数组和委托的方法,并返回 {1,4,9}.类似的东西已经存在了吗?

                I'm wondering if there is built-in .NET functionality to change each value in an array based on the result of a provided delegate. For example, if I had an array {1,2,3} and a delegate that returns the square of each value, I would like to be able to run a method that takes the array and delegate, and returns {1,4,9}. Does anything like this exist already?

                推荐答案

                我不知道(替换每个元素而不是转换为新的数组或序列),但它非常容易编写:

                Not that I'm aware of (replacing each element rather than converting to a new array or sequence), but it's incredibly easy to write:

                public static void ConvertInPlace<T>(this IList<T> source, Func<T, T> projection)
                {
                    for (int i = 0; i < source.Count; i++)
                    {
                        source[i] = projection(source[i]);
                    }
                }
                

                用途:

                int[] values = { 1, 2, 3 };
                values.ConvertInPlace(x => x * x);
                

                当然,如果您真的 需要 更改现有数组,则使用 Select 发布的其他答案会更实用.或 .NET 2 中现有的 ConvertAll 方法:

                Of course if you don't really need to change the existing array, the other answers posted using Select would be more functional. Or the existing ConvertAll method from .NET 2:

                int[] values = { 1, 2, 3 };
                values = Array.ConvertAll(values, x => x * x);
                

                这都是假设一个一维数组.如果你想包含矩形数组,它会变得更棘手,特别是如果你想避免装箱.

                This is all assuming a single-dimensional array. If you want to include rectangular arrays, it gets trickier, particularly if you want to avoid boxing.

                这篇关于C#:更改数组中每个项目的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                    <tbody id='dzFhH'></tbody>

                  1. <tfoot id='dzFhH'></tfoot>

                      <bdo id='dzFhH'></bdo><ul id='dzFhH'></ul>

                        <small id='dzFhH'></small><noframes id='dzFhH'>

                          <i id='dzFhH'><tr id='dzFhH'><dt id='dzFhH'><q id='dzFhH'><span id='dzFhH'><b id='dzFhH'><form id='dzFhH'><ins id='dzFhH'></ins><ul id='dzFhH'></ul><sub id='dzFhH'></sub></form><legend id='dzFhH'></legend><bdo id='dzFhH'><pre id='dzFhH'><center id='dzFhH'></center></pre></bdo></b><th id='dzFhH'></th></span></q></dt></tr></i><div id='dzFhH'><tfoot id='dzFhH'></tfoot><dl id='dzFhH'><fieldset id='dzFhH'></fieldset></dl></div>
                          <legend id='dzFhH'><style id='dzFhH'><dir id='dzFhH'><q id='dzFhH'></q></dir></style></legend>