我是C#的新手,我無法理解爲什麼這段代碼不起作用。交換數組中2個元素的函數不起作用
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」。我能夠找到解決這個問題的不同方法,但我想知道的是這個代碼中的錯誤。 感謝您的幫助!
感謝,認爲C#做它總是由參,問題溶膠VED。 – user2373458 2013-05-11 18:24:58