Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数组传递给特定类型的函数_C# - Fatal编程技术网

C# 将数组传递给特定类型的函数

C# 将数组传递给特定类型的函数,c#,C#,我需要这个代码的帮助。我不能让它这样工作。。有些事情我还不清楚 using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static int[] foo(int[] array) { int[] xx = new int[array.Length]; for(int i = 0; i < arr

我需要这个代码的帮助。我不能让它这样工作。。有些事情我还不清楚

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution
{
    static int[] foo(int[] array)
    {
        int[] xx = new int[array.Length];

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

        return array;
    }

    static void Main(string[] args)
    {

        int[] array = new int[4] {73, 67, 38, 33 };

        foo(array);

    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
类解决方案
{
静态int[]foo(int[]数组)
{
int[]xx=新的int[array.Length];
for(int i=0;i
如何重建foo函数以从数组返回值

我看到了很多关于void函数的帮助链接。。它们也没用。当涉及到特定类型的功能时,我不能让它工作

谢谢


ddr8

您当前的代码确实返回了一个数组,但您是:

  • 更新
    数组中的值,而不是新的
    xx
    数组
  • 未在
    Main
  • 此处是修改现有阵列而不是新阵列的位置:

    static int[] foo(int[] array)
    {
        int[] xx = new int[array.Length];
    
        for(int i = 0; i < array.Length; i++)
        {
            // NOTE: modifying array, not xx
            array[i] *= 2;
            Console.WriteLine(array[i]);
        }
    
        // NOTE: returning array, not xx -- xx is not used
        return array;
    }
    
    如果需要更改数组大小,另一个选项是将数组作为
    ref
    参数传递:

    static int[] foo(ref int[] array)
    {
        // Modify array as necessary
    }
    

    如果希望从
    foo()
    返回值,只需将其分配给一个变量:
    int[]fooResult=foo(数组)
    foo正在做的一些事情是值得怀疑的,但是它正在返回数组,并且已经返回了数组中的值,不是吗?你能再解释一下你的要求吗?如果你这样做的话,
    //Console.WriteLine(数组[i])将不打印任何内容。即使我添加了一个变量,您刚才显示的变量仍然没有返回值。谢谢。你的代码有效。。我需要练习使用函数,我认为这是一个完全错误的方式。不用担心,这不是一个简单的概念,但一旦它点击它点击。查看这些链接我喜欢dotnetpearls他们有很好的例子谢谢你的帮助和建议!:)谢谢你,这很有帮助!
    
    static int[] foo(ref int[] array)
    {
        // Modify array as necessary
    }
    
        class Solution
        {
            static int[] foo(int[] array)
            {
                int[] xx = new int[array.Length]; // Building this but not using it
    
                for (int i = 0; i < array.Length; i++)
                {
                    array[i] *= 2; //Altering the input array changes the array in the Main method..
    
                    Console.WriteLine(array[i]);
                }
    
                return array;
            }
    
            static void Main(string[] args)
            {
    
    
                int[] array = new int[4] { 73, 67, 38, 33 };
    
                foo(array); // Not assigning the values to an object.
                //After this step look at array it will be 146, 134, 76 , 66
            }
        }
    
        class Solution
        {
            static int[] foo(int[] array)
            {
    
            int[] xx = new int[array.Length];
            //Copy the array into the new array
            Array.Copy(array, xx, array.Length);  
    
                for (int i = 0; i < xx.Length; i++)
                {
                    xx[i] *= 2; 
    
                    Console.WriteLine(xx[i]);
                }
                return xx;
            }
    
            static void Main(string[] args)
            {
                int[] array = new int[4] { 73, 67, 38, 33 };
                //Assign the new array to an object
                int[] newArray = foo(array);
            }
        }
    
                static void Main(string[] args)
                {
                            int[] array = new int[4] { 73, 67, 38, 33 };
                            int[] newarr = array.Select(arrayvalue => arrayvalue * 2).ToArray();
                }