+ 1
Can anyone expalin this method?
public static string ReverseString(string s) { char[] arr = s.ToCharArray(); Array.Reverse(arr); return new string(arr); }
6 Answers
+ 7
* method takes a String
* char[] arr = s.toCharArray() creates a char array of your String
//"hello" --> ['h', 'e', 'l', 'l', 'o']
* Array.Reverse(arr) reverses this array
//['o', 'l', 'l', 'e', 'h']
* new String(arr) creates a String of your array (which is reversed now)
// ['o', 'l', 'l', 'e', 'h'] --> "olleh"
* method returns this string
+ 6
Here is a description of the String class: https://www.google.de/amp/s/www.geeksforgeeks.org/c-sharp-string-class/amp/
+ 5
Without new, it won't work. String is a class and with the keyword new you create an instance of this class (you create an object).
https://en.m.wikibooks.org/wiki/C_Sharp_Programming/Keywords/new
+ 1
Daniel Adam i know but i want to know how this works.
+ 1
Denise RoĂberg thanks can you tell me why we use here new keyword.can we run this function without new.
I never use new when i return something from function.sorry am noob in c#.đđđ€Ł
0
It's doing what it promised with it's function name: reverse a string.