C#数组做参数问题

ITurbo ITurbo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
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)
{
arr = (arr.Reverse()).ToArray();
PrintArray(arr);
System.Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]);
// return arr;
}

static void ChangeArrayElements(string[] arr)
{
arr[0] = "Sat";
arr[1] = "Fri";
arr[2] = "Thu";

System.Console.WriteLine("arr[0] is {0} in ChangeArrayElements.", arr[0]);
}

static void Main()
{
// Declare and initialize an array.
string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
PrintArray(weekDays);

ChangeArray(weekDays);
System.Console.WriteLine("Array weekDays after the call to ChangeArray:");
PrintArray(weekDays);
System.Console.WriteLine();

ChangeArrayElements(weekDays);
System.Console.WriteLine("Array weekDays after the call to ChangeArrayElements:");
PrintArray(weekDays);
}
}
}
问题是红色的代码都改变了arr的值,那为什么返回后weekDays前面没改变,后面改变了呢?
结果是这样的
...全文
评论 15
收藏 已收藏
分享
返回顶部
ITurbo
C# 111,130 成员 642,540 内容