+ 1

Can anyone expalin this method?

public static string ReverseString(string s) { char[] arr = s.ToCharArray(); Array.Reverse(arr); return new string(arr); }

14th May 2019, 11:50 AM
Maninder $ingh
Maninder $ingh - avatar
6 Respostas
+ 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
14th May 2019, 2:13 PM
Denise Roßberg
Denise Roßberg - avatar
14th May 2019, 3:00 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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
14th May 2019, 2:57 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Daniel Adam i know but i want to know how this works.
14th May 2019, 1:38 PM
Maninder $ingh
Maninder $ingh - avatar
+ 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#.😂😂🤣
14th May 2019, 2:46 PM
Maninder $ingh
Maninder $ingh - avatar
0
It's doing what it promised with it's function name: reverse a string.
14th May 2019, 12:51 PM
Daniel Adam
Daniel Adam - avatar