C# 用于交换数组中2个元素的函数';行不通

C# 用于交换数组中2个元素的函数';行不通,c#,swap,C#,Swap,我是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) {

我是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”。我能够找到不同的方法来解决这个问题,但我想知道的是这段代码中的错误是什么。
谢谢你的帮助

您正在按值传递参数
a
b

有关详细信息,请参阅

这里有两种解决方案来解决您的问题

//Pass by value and return the values
static Tuple<char, char> swap2(char a, char b)
{
    char temp = a;
    a = b;
    b = temp;
    return new Tuple<char, char>(a, b);
}

//Pass by reference
static void swap3(ref char a, ref char b)
{
    char temp = a;
    a = b;
    b = temp;
}

public static void Main(string[] args)
{
    char[] sw2 = "ab".ToCharArray();
    var chars2 = swap2(sw2[0], sw2[1]);
    sw2[0] = chars2.Item1;
    sw2[1] = chars2.Item2;
    //Will print "ba"
    Console.WriteLine(sw2);

    char[] sw3 = "ab".ToCharArray();
    swap3(ref sw3[0], ref sw3[1]);
    //Will print "ba"
    Console.WriteLine(sw3);
}
//传递值并返回值
静态元组swap2(字符a、字符b)
{
字符温度=a;
a=b;
b=温度;
返回新的元组(a,b);
}
//参照
静态空隙swap3(参考字符a、参考字符b)
{
字符温度=a;
a=b;
b=温度;
}
公共静态void Main(字符串[]args)
{
char[]sw2=“ab”.ToCharArray();
var chars2=swap2(sw2[0],sw2[1]);
sw2[0]=chars2.Item1;
sw2[1]=chars2.Item2;
//将打印“ba”
控制台写入线(sw2);
char[]sw3=“ab”。ToCharArray();
swap3(参考sw3[0],参考sw3[1]);
//将打印“ba”
控制台写入线(sw3);
}
这里有一个关于是否应该使用或尝试避免ref关键字的问题。除了最简单的用途外,通常建议尽可能避免ref。Swap属于“最简单使用”的范畴,但我建议您在大多数实际情况下尽量避免使用ref

问题在于
交换方法实际上只是处理
a
b
的本地副本。您需要通过引用传递参数。因此,您可以这样定义
swap
方法:

    static void swap(ref char a, ref char b)
    {
        char temp = a;
        a = b;
        b = temp;
    }
    swap(ref sw[0], ref sw[1]);
这样称呼它:

    static void swap(ref char a, ref char b)
    {
        char temp = a;
        a = b;
        b = temp;
    }
    swap(ref sw[0], ref sw[1]);

交换是采用两种值类型,并在变量之间交换值。没有任何东西会修改原始数组。您需要将交换方法修改为以下内容:

static void Swap(char[] array, int a, int b)
{
    char temp = array[a];
    array[a] = array[b];
    array[b] = temp;
}
然后可以从Main()调用它,如:


它应该按照以下方式进行修改(注意:在本例中,
ref char[]arr
前缀为
ref
,主要用于教学目的:默认情况下,数组将通过
ref
传递)


更通用的数组交换函数:

    public static void Swap<T>(this T[] array, int indexA, int indexB)
    {
        T temp        = array[indexA];
        array[indexA] = array[indexB];
        array[indexB] = temp;
    }
公共静态无效交换(此T[]数组,int indexA,int indexB)
{
T temp=数组[indexA];
数组[indexA]=数组[indexB];
数组[indexB]=温度;
}
此外,还提供了交换多个数组元素的通用函数:

    public static void Swap<T>(this T[] array, int indexA, int indexB, int length)
    {
        while (length-- > 0)
            Swap(array, indexA++, indexB++);
    }
公共静态无效交换(此T[]数组,int indexA,int indexB,int length)
{
while(长度-->0)
交换(数组,indexA++,indexB++);
}

这可能是正确的选择@亚历克斯贝尔我同意这可能是交换的方式,因为它太简单了。但我通常会尽量避免使用ref关键字,因为它往往会导致messier API IMO。这正是我要做的,但要使
Swap
generic,使其适用于任何数组,然后将其放入一些实用程序类中。