none
MSDN中的『C#编程指南』展示代码中出现问题

    问题

  • 在学习C#的过程中,发现在『C#编程指南』的小节 『将数组作为参数传递』中(https://msdn.microsoft.com/zh-cn/library/hyfeyz71.aspx)。在执行 arr = (arr.Reverse()).ToArray();时报错,因为是刚学,所以只查到reverse是static的,好像只应该接在Array.后面,那么在C#教学中的代码还对嘛,毕竟是示范代码啊!

    (因为是新帐号,所以不能显示照片,抱歉!)

    2016年8月5日 7:07

答案

  • 你好 Lian_iz,

    根据你的描述, 我做了一个demo(代码如下), 这个Reverse() 方法, 需要using System.Linq

    using System.Linq;
    
    namespace ArrayClass
    {
        class ArrayClass
        {
            static void PrintArray(string[] arr)
            {
                for (int i = 0; i < arr.Length; i++)
                {
                    System.Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " : "");
                }
                System.Console.WriteLine();
            }
    
            static void ChangeArray(string[] arr)
            {
                // The following attempt to reverse the array does not persist when
                // the method returns, because arr is a value parameter.
                arr = (arr.Reverse()).ToArray();
                // The following statement displays Sat as the first element in the array.
                System.Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]);
            }
    
            static void ChangeArrayElements(string[] arr)
            {
                // The following assignments change the value of individual array 
                // elements. 
                arr[0] = "Sat";
                arr[1] = "Fri";
                arr[2] = "Thu";
                // The following statement again displays Sat as the first element
                // in the array arr, inside the called method.
                System.Console.WriteLine("arr[0] is {0} in ChangeArrayElements.", arr[0]);
            }
    
            static void Main(string[] args)
            {
                // Declare and initialize an array.
                string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    
                // Pass the array as an argument to PrintArray.
                PrintArray(weekDays);
    
                // ChangeArray tries to change the array by assigning something new
                // to the array in the method. 
                ChangeArray(weekDays);
    
                // Print the array again, to verify that it has not been changed.
                System.Console.WriteLine("Array weekDays after the call to ChangeArray:");
                PrintArray(weekDays);
                System.Console.WriteLine();
    
                // ChangeArrayElements assigns new values to individual array
                // elements.
                ChangeArrayElements(weekDays);
    
                // The changes to individual elements persist after the method returns.
                // Print the array, to verify that it has been changed.
                System.Console.WriteLine("Array weekDays after the call to ChangeArrayElements:");
                PrintArray(weekDays);
    
                System.Console.ReadKey();
    
            }
        }
    }
    

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年8月5日 8:33
    版主

全部回复

  • 执行 arr = (arr.Reverse()).ToArray();时报错

    请问一下是什么错误?


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年8月5日 8:27
  • 你好 Lian_iz,

    根据你的描述, 我做了一个demo(代码如下), 这个Reverse() 方法, 需要using System.Linq

    using System.Linq;
    
    namespace ArrayClass
    {
        class ArrayClass
        {
            static void PrintArray(string[] arr)
            {
                for (int i = 0; i < arr.Length; i++)
                {
                    System.Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " : "");
                }
                System.Console.WriteLine();
            }
    
            static void ChangeArray(string[] arr)
            {
                // The following attempt to reverse the array does not persist when
                // the method returns, because arr is a value parameter.
                arr = (arr.Reverse()).ToArray();
                // The following statement displays Sat as the first element in the array.
                System.Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]);
            }
    
            static void ChangeArrayElements(string[] arr)
            {
                // The following assignments change the value of individual array 
                // elements. 
                arr[0] = "Sat";
                arr[1] = "Fri";
                arr[2] = "Thu";
                // The following statement again displays Sat as the first element
                // in the array arr, inside the called method.
                System.Console.WriteLine("arr[0] is {0} in ChangeArrayElements.", arr[0]);
            }
    
            static void Main(string[] args)
            {
                // Declare and initialize an array.
                string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    
                // Pass the array as an argument to PrintArray.
                PrintArray(weekDays);
    
                // ChangeArray tries to change the array by assigning something new
                // to the array in the method. 
                ChangeArray(weekDays);
    
                // Print the array again, to verify that it has not been changed.
                System.Console.WriteLine("Array weekDays after the call to ChangeArray:");
                PrintArray(weekDays);
                System.Console.WriteLine();
    
                // ChangeArrayElements assigns new values to individual array
                // elements.
                ChangeArrayElements(weekDays);
    
                // The changes to individual elements persist after the method returns.
                // Print the array, to verify that it has been changed.
                System.Console.WriteLine("Array weekDays after the call to ChangeArrayElements:");
                PrintArray(weekDays);
    
                System.Console.ReadKey();
    
            }
        }
    }
    

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年8月5日 8:33
    版主
  • Lian:

    补充一点:你应该到此处申请通过账号审核(必须英文,但可以随便复制别人的审核的话)。

    https://social.microsoft.com/Forums/en-US/338e5c21-c7e3-4519-8791-f819535be481/verify-your-account-36?forum=reportabug


    ASP.NET Forum
    Other Discussion Forums
    FreeRice Donate
    Issues to report
    Free Tech Books Search and Download

    2016年8月5日 10:01
    版主