将数组值传递给方法 [英] pass array values to a method

查看:89
本文介绍了将数组值传递给方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好



第一次发布。我是C#noob,还在学习。



我正在尝试编写一个简单的控制台应用程序来初始化一个数组,然后将这些值传递给一个方法然后将该值乘以5并将结果输出到控制台....

我似乎无法得到正确的任何帮助将被赞赏,下面是我到目前为止,但我得到异常错误告诉我索引超出范围??



Hi Guys

firs time posting. I'm a C# noob, and still learning.

I am trying to write a simple console app that will initialize an array, then pass those values to a method which will then multiply the value by 5 and output the result to the console....
I can't seem to get this right any help will be appreciated, below is what I have so far but I get an exception error telling me the index is out of range??

namespace ConsoleApplication5
{
    class Program
    {

        static void mx(int[] numbers)
        {
            foreach (int i in numbers)
            {
                numbers[i] = numbers[i]*5;

                Console.WriteLine(numbers[i]);
            }
        }

        static void Main()
        {
            int[] numbers = new int[10] {1,2,3,4,5,6,7,8,9,10};

            Program.mx(numbers);

            Console.ReadLine();

        }




    }
}

推荐答案

C#数组从0开始,您尝试使用此代码迭代超出定义数组的限制/大小 - > numbers [i] = numbers [i] * 5;

所以你得到一个超出范围异常的索引。



修改如下





C# array starts from 0 and you are trying to iterate outside the limit/size of the defined array with this code -> numbers[i] = numbers[i]*5;
So you get an index out of range exception.

Modify as below


class Program
       {

           static void mx(int[] numbers)
           {
               foreach (int i in numbers)
               {
                  // numbers[i] = numbers[i] * 5;

                   Console.WriteLine(i * 5);
               }
           }

           static void Main()
           {
               int[] numbers = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

               Program.mx(numbers);

               Console.ReadLine();

           }




       }


你使用一个foreach循环。这枚举了数组中的元素。然后使用数字作为索引。数组中的最后一个数字是10,这是无效索引,因为数组的下限为0,上限为9.只要您不关心更新原始数组,Mathew的解决方案就可以了。这是另一种方法:



You use a foreach loop. This enumerates the elementes inside the array. Then you use the number as index. Last number in your array is 10, this is invalid index because lower bound of the array is 0 and upper bound is 9. Mathew's solution is fine as long as you don't care for updating the original array. Here is an alternative which does that:

for (int i=0; i < numbers.Length; i++)
{
  numbers[i] = numbers[i]*5; 
  Console.WriteLine(numbers[i]);
}


此示例使用Lambda表示法分配一个Action Delegate,迭代器函数(ForEach)应用于它迭代的List中的每个元素:
This example uses Lambda notation to assign an Action Delegate that the iterator Function (ForEach) applies to each element in the List it iterates over:
// requires System.Collections.Generic
// does not require Linq
// 'Collection.ForEach available as of .NET 2.0, will not work with an Array
static void Main()
{
    new List<int>{1,2,3,4,5,6,7,8,9,10}.ForEach(i => Console.WriteLine(i * 5));

    Console.ReadLine();
}</int>

如果这个例子让你感到困惑,请告诉我,我会将其删除。

If this example confuses you, let me know, and I'll remove it.


这篇关于将数组值传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆