C
C#3y ago
Ryan-T1412

❔ ✅ using multiple methods to object

.
17 Replies
Ryan-T1412
Ryan-T1412OP3y ago
ok so before reversing the string we should convert it to an array, right? why it's not number.ToArray().Reverse()
Thinker
Thinker3y ago
string already implements IEnumerable<char> so there's no need to
MODiX
MODiX3y ago
thinker227#5176
REPL Result: Success
"foo bar".Reverse()
"foo bar".Reverse()
Result: ReverseIterator<char>
[
"r",
"a",
"b",
" ",
"o",
"o",
"f"
]
[
"r",
"a",
"b",
" ",
"o",
"o",
"f"
]
Compile: 436.640ms | Execution: 25.682ms | React with ❌ to remove this embed.
Thinker
Thinker3y ago
Also new string(numb.ToString()) isn't necessary, you can just do numb.ToString()
Ryan-T1412
Ryan-T1412OP3y ago
how about next line ?
string reversedNumber = new string(number.Reverse().ToArray());
string reversedNumber = new string(number.Reverse().ToArray());
if i remove new string it gaves an error
Thinker
Thinker3y ago
You need it there because ToArray() returns a char[], and new string creates a string from a char[] .
Ryan-T1412
Ryan-T1412OP3y ago
i see
string reversedNumber = number.Reverse();
string reversedNumber = number.Reverse();
but what if i do it like this insted of converting string -> array -> string
Thinker
Thinker3y ago
That won't work because you can't convert an IEnumerable<char> to a string.
MODiX
MODiX3y ago
thinker227#5176
REPL Result: Failure
string reversed = "123".Reverse();
string reversed = "123".Reverse();
Exception: CompilationErrorException
- Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<char>' to 'string'. An explicit conversion exists (are you missing a cast?)
- Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<char>' to 'string'. An explicit conversion exists (are you missing a cast?)
Compile: 437.548ms | Execution: 0.000ms | React with ❌ to remove this embed.
ero
ero3y ago
Now i wonder what the fastest way to reverse is...
Thinker
Thinker3y ago
string.Create probably
ero
ero3y ago
Because it always is But there's more nuance to it Because you don't necessarily have to turn the number into a string first
Thinker
Thinker3y ago
however, that was not the question, so let's not get sidetracked catsip
ero
ero3y ago
I wanna side track!
Thinker
Thinker3y ago
go to #allow-unsafe-blocks when
Ryan-T1412
Ryan-T1412OP3y ago
thank you !close
Accord
Accord3y ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?