❔ I don´t understand this line of code at all (random char letter)
Hi, I am finding it really hard to understand how its possible to randomize a letter like this:
Random rnd = new Random();
char randomChar = (char)rnd.Next('a', 'z');
I thought that the Next() method only could take int's as a parameter?
12 Replies
Chars have an implicit conversion to int
Because that's basically what they are
The character's location on the ASCII table
doesn´t the (char) make it an explicit conversion?
Yes, the resulting int is the explicitly converted
char->int is implicit
int->char is explicit
so just to be clear,
char randomChar = (char)rnd.Next('a', 'z'); is an explicit conversion, and a and z already are represented by numbers in the ASCII table?
The bit about the ASCII table is why chars are basically ints
'a'
and 'z'
can be used in place of itegers because an implicit conversion existsAngius
REPL Result: Success
Compile: 421.767ms | Execution: 27.313ms | React with ❌ to remove this embed.
Angius
REPL Result: Failure
Exception: CompilationErrorException
Compile: 476.993ms | Execution: 0.000ms | React with ❌ to remove this embed.
As you can see
Implcit conversion works one way but not the other
so the (char) conversion is to make sure that the variable randomChar is converted? while the 'a' and 'z' parameters are not necessary to convert?
Basically
ok, thanks!
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.