Posts

Showing posts with the label Array

Reverse array in C#

To reverse an array we can use Reverse method of Array class. Overloads This method has two overloads. First overload takes an array as parameter which will be reversed. Second overload takes 3 parameters. First is array second is start index from where to start and third is length. string []arr = new string [3]; arr[0] = "One" ; arr[1] = "Two" ; arr[2] = "Three" ; Console .WriteLine( "Array before reversing" ); foreach ( string str in arr) {     Console .WriteLine(str); } Array .Reverse(arr); Console .WriteLine( "Array after reversing" ); foreach ( string str in arr) {     Console .WriteLine(str); } Output Array before reversing One Two Three Array after reversing Three Two One             

Using string array in C#

To create a stirng array we can define array of string type and length of array. string []arr = new string [5]; We can assign value to array by using index of array. Similarly we can get value by using index of array. Assigning value to array arr[0] = "One" ; arr[1] = "Two" ; arr[2] = "Three" ; arr[3] = "Four" ; arr[4] = "Five" ; Getting value from array string val = arr[2];