我是C#的新手,但不明白为什么这段代码不能运行。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
char[] sw = "ab".ToCharArray();
swap(sw[0], sw[1]);
string end = new string(sw);
Console.Write(end);
}
static void swap(char a, char b)
{
char temp = a;
a = b;
b = temp;
}
}
}
我期望在控制台上看到的是"ba",但是实际上却得到了"ab"。我找到了其他解决此问题的方法,但我想知道这段代码中的错误在哪里。 感谢您的帮助!
Swap
成为通用的,以便它适用于任何数组,然后将其放入某些实用程序类中。 - Lukazoid