DQbJA%{/pPPhy(Py/h'{8pUzUyU@i8pU5a8?P/Phii{/pPUyhS{k/yU{k/UyU{8Uy{8KqPU{4P?czUii{8pUiyU{8UyU{j8Uy{j8aAabBAeEDAfCWZCCB| 9976666696666VZWBZCYZCBXC

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. `You have to implement a custom swap method for string characters and other types.`Example.` 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 `int`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 `string-constructor`The program` defines two swap implementations. The SwapCharacters method receives three parameters: a reference to a string, and the first position and the second position of the values you want to swap.`SwapCharacters` invokes the ToCharArray method, which returns the logical char buffer stored internally in the string. It uses a temporary variable, assigns the positions, and returns a new string. `ToCharArray `tochararray`SwapInts returns no reference.` It is a void method. It receives three parameters, the first being an array reference with elements of type Int32, and the two positions you want to exchange. `It uses a temporary variable, but the int array is modified in-place so no reference must be returned.`Int Array `int-array`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 `if`This negatively impacts performance in tight loops. It is only necessary when many callers will exist.`ArgumentNullException `argumentexception`IndexOutOfRangeException `indexoutofrangeexception`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 `xor`However: `As Wikipedia notes it is now much slower than using a normal temporary variable.`XOR swap algorithm: Wikipedia `https://en.wikipedia.org/wiki/XOR_swap_algorithm`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. `We noted the XOR swap algorithm and its drawbacks in modern programming languages and optimizing compilers.

678 77777 { 676{8 66// 667Swap c7s7the 7. 66// 6687 717"7"; 667 swap178SwapC7s8(71, 0, 1); 667swap1);8 66// 667Swap 7s77. 66// 6687[] 717{ 1, 2, 3, 4, 5 }; 668SwapInts8(71, 0, 1); 667 (77771) 66{ 6667.7(7); 66} 6} 677 8SwapC7s8(7 7, 7position1, 7position2) 6{8 66// 667Swaps c7s7a 7. 66// 668char[] 777.ToChar7();8 77c7s 668char temp77[position1];8 77temporary copy of c7 6687[position1]77[position2];8 7Assign 7 6687[position2]7temp;8 7Assign 7 6687 77(7);8 777 68} 6778SwapInts8(7[] 7, 7position1, 7position2) 6{8 66// 667Swaps 7s7an 7. 66// 6687temp77[position1];8 7Copy the first position's 7 6687[position1]77[position2];8 7Assign7the second 7 6687[position2]7temp;8 7Assign7the first 7 68} } 8 oDt Net Perls 213458

!Cswaps characters and array elements