Getting Started With C#
Literals
Operators
Conditional Statements
Equality Operator
Equals and GetHashCode
Null-Coalescing Operator
Null-Conditional Operators
nameof Operator
Verbatim String
Common String Operations
String Format
String Concatenate
String Manipulation
String Interpolation
String Escape Sequences
StringBuilder
Regex
DateTime
Arrays
On Algorithm for circular rotation of an array
Enum
Tuples
Overview of C# collections
GUID
BigInteger
Collection Initializers
Looping
Iterators
IEnumerable
Value type vs Reference type
Built-in Types
Aliases of built-in types
Anonymous types
Dynamic type
Type Conversion
Casting
Nullable types
Constructors and Finalizers
Access Modifiers
Interfaces
Static Classes
Singleton Implementation
Dependency Injection
Partial class and methods
Object Initializers
Methods
Extension Methods
Named Arguments
Named And Optional Arguments
Data Annotation
Keywords
Recursion
Naming Conventions
XML Documentation Comments
Comments and regions
Inheritance
Generics
Using Statement
Using Directive
IDisposable interface
Reflection
IQueryable interface
LINQ to Objects
LINQ Queries
LINQ to XML
Parallel LINQ PLINQ
XmlDocument
XDocument
C# 7.0 Features
C# 6.0 Features
C# 5.0 Features
C# 4.0 Features
C# 3.0 Features
Exception Handling
NullReferenceException
Handling FormatException
Read and Understand Stack trackes
Diagnostics
Overflow
JSON handling
Using json.net
Lambda Expressions
Lambda expressions
Generic Lambda Query Builder
Properties
Initializing Properties
INotifyPropertyChanged interface
Events
Expression Trees
Overload Resolution
Preprocessor directives
Structs
Attributes
Delegates
File and Stream IO
Networking
Performing HTTP requests
Reading and writing .zip files
FileSystemWatcher
Asynchronous Socket
Action Filters
Polymorphism
Immutability
Indexer
Checked and Unchecked
Stream
Timers
Stopwatches
Threading
Async/Await, BackgroundWorker, Task and Thread examples
Async Await
Synchronization Context in Async/Await
BackgroundWorker
Task Parallel Library
Making a variable thread safe
Lock Statement
Yield Keyword
Task Parallel Library TPL Dataflow Constructs
Functional Programming
Func delegates
Function with multiple return values
Binary Serialization
ICloneable
IComparable
Accessing Databases
Using SQLite
Caching
Code Contracts
Code Contracts and Assertions
Structural Design Patterns
Creational Design Patterns
Implementing Decorating Design Pattern
Implementing Flyweight Design Pattern
System.Management.Automation
Pointers
Pointers and Unsafe Code
Simulating C Unions with C# Structs
Reactive Extensions Rx
AsseblyInfo.cs examples
Creating Console Application
CLSCompliantAttribute
ObservableCollection<T>
Hash Functions
Generating Random Numbers
Cryptography
Unsafe Code in .NET
C# Script
Runtime Compile
Interoperability
.NET Compiler Platform Roslyn
Creating Own MessageBox in Windows Form Application
Including Font Resources
Garbage Collector
Windows Communication Foundation
Contributors

Example of a generic method that rotates an array by a given shift

suggest change
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<T>(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;
}

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.

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)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents