C# Swap Methods

This C# example program swaps two values in an array and a string.
Swap exchanges array element values. It acts on two separate elements so that they both are still present but in opposite locations. In the C# language, there is no built-in method for this purpose on most types.

Tip: You have to implement a custom swap method for string characters and other types.

This example shows how to swap two characters in a string's character buffer. It also shows how to swap two elements in an array with elements of type Int32. The method that swaps string characters is more complex.Int, uint

Note: The string type is immutable and you cannot assign characters to locations in a string directly.

So: For the string swap method, we use the ToCharArray method and the new string constructor and return a new string reference.

String Constructor

SwapCharacters: This invokes the ToCharArray method. It uses a temporary variable, assigns the positions, and returns a new string.

ToCharArray

SwapInts: This uses a temporary variable, but the int array is modified in-place so no reference must be returned.

Int Array
C# program that swaps characters and array elements using System; class Program { static void Main() { // // Swap characters in the string. // string value1 = "Dot Net Perls"; string swap1 = SwapCharacters(value1, 0, 1); Console.WriteLine(swap1); // // Swap elements in array. // int[] array1 = { 1, 2, 3, 4, 5 }; SwapInts(array1, 0, 1); foreach (int element in array1) { Console.Write(element); } } static string SwapCharacters(string value, int position1, int position2) { // // Swaps characters in a string. // char[] array = value.ToCharArray(); // Get characters char temp = array[position1]; // Get temporary copy of character array[position1] = array[position2]; // Assign element array[position2] = temp; // Assign element return new string(array); // Return string } static void SwapInts(int[] array, int position1, int position2) { // // Swaps elements in an array. // int temp = array[position1]; // Copy the first position's element array[position1] = array[position2]; // Assign to the second element array[position2] = temp; // Assign to the first element } } Output oDt Net Perls 21345
Exceptions. This code will cause exceptions if you call it with invalid parameters. If you are developing a library function that will have many callers, you can validate the arguments with if-statements and then throw.If

Note: This negatively impacts performance in tight loops. It is only necessary when many callers will exist.

ArgumentExceptionIndexOutOfRangeException
XOR. An interesting bitwise algorithm uses the XOR bitwise operator "^=" in the C# language to swap two elements. In computing history, this method was faster because it reduced the number of registers in use.XOR

However: As Wikipedia notes it is now much slower than using a normal temporary variable.

XOR swap algorithm: Wikipedia
I have confirmed this in the C# language and the C programming language using GCC. In the C# language, bitwise operators are not the focus of the optimization efforts in the JIT. They thus may be neglected in optimization.
Summary. We saw methods that swap characters in a string or elements in an array. These methods were tested and proven correct on the input provided. We also discussed problems with swapping algorithms such as exceptions.

And: We noted the XOR swap algorithm and its drawbacks in modern programming languages and optimizing compilers.

© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls