Example of a generic method that rotates an C# with Example



Example of a generic method that rotates an C# with Example

array by a given shift 
I would like to point out that we rotate left when the shifting value is negative and we rotate right when the value is 
positive. 
public static void Main() 
{ 
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
int shiftCount = 1; 
Rotate(ref array, shiftCount); 
Console.WriteLine(string.Join(", ", array)); 
// Output: [10, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
array = new []{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
shiftCount = 15; 
Rotate(ref array, shiftCount); 
Console.WriteLine(string.Join(", ", array)); 
// Output: [6, 7, 8, 9, 10, 1, 2, 3, 4, 5] 
array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
shiftCount = -1; 
Rotate(ref array, shiftCount); 
Console.WriteLine(string.Join(", ", array)); 
// Output: [2, 3, 4, 5, 6, 7, 8, 9, 10, 1] 
array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
shiftCount = -35; 
Rotate(ref array, shiftCount); 
Console.WriteLine(string.Join(", ", array)); 
// Output: [6, 7, 8, 9, 10, 1, 2, 3, 4, 5] 
} 
private static void Rotate(ref T[] array, int shiftCount) 
{ 
T[] backupArray= new T[array.Length]; 
for (int index = 0; index < array.Length; index++) 
{ 
backupArray[(index + array.Length + shiftCount % array.Length) % array.Length] = 
array[index]; 
} 
array = backupArray; 
} 
The thing that is important in this code is the formula with which we find the new index value after the rotation. 
(index + array.Length + shiftCount % array.Length) % array.Length 
 

Here is a little more information about it: 
(shiftCount % array.Length) -> we normalize the shifting value to be in the length of the array (since in an array 
with length 10, shifting 1 or 11 is the same thing, the same goes for -1 and -11). 
array.Length + (shiftCount % array.Length) -> this is done due to left rotations to make sure we do not go into a 
negative index, but rotate it to the end of the array. Without it for an array with length 10 for index 0 and a rotation 
-1 we would go into a negative number (-1) and not get the real rotation index value, which is 9. (10 + (-1 % 10) = 9) 
index + array.Length + (shiftCount % array.Length) -> not much to say here as we apply the rotation to the index 
to get the new index. (0 + 10 + (-1 % 10) = 9) 
index + array.Length + (shiftCount % array.Length) % array.Length -> the second normalization is making sure 
that the new index value does not go outside of the array, but rotates the value in the beginning of the array. It is 
for right rotations, since in an array with length 10 without it for index 9 and a rotation 1 we would go into index 10, 
which is outside of the array, and not get the real rotation index value is 0. ((9 + 10 + (1 % 10)) % 10 = 0) 
 

An enum can derive from any of the following types: byte, sbyte, short, ushort, int, uint, long, ulong. The default is 
int, and can be changed by specifying the type in the enum definition: 
public enum Weekday : byte { Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5 } 
This is useful when P/Invoking to native code, mapping to data sources, and similar circumstances. In general, the 
default int should be used, because most developers expect an enum to be an int. 

0 Comment's

Comment Form

Submit Comment