Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange scenario Convert.ToChar(integer) or (char)integer

Tags:

c#

I have the following condition.

Console.WriteLine(Convert.ToChar(65)); // Gives 'A'

Console.WriteLine(Convert.ToChar(65) + 1); // Gives 66; Expectation was 'A1'

Console.WriteLine(Convert.ToChar(65) + "1"); // Gives 'A1'

Why is that? I wanted the 3rd case.

like image 570
Ashokan Sivapragasam Avatar asked Jul 21 '26 16:07

Ashokan Sivapragasam


1 Answers

Reason you are getting 66 instead of A1 is because you are adding a char to an integer. If you add a string to char, only then it will return you a string.

documentation on char The char keyword is used to declare an instance of the System.Char structure that the .NET Framework uses to represent a Unicode character. The value of a Char object is a 16-bit numeric (ordinal) value

like image 139
Jawad Avatar answered Jul 24 '26 09:07

Jawad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!