0
I was expecting for the output to be 'b', why does it outputs 98? And how can i make the output letter 'b'?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sololearn { class Program { static void Main(string[] args) { char letter = 'a'; Console.Write(letter+1); } } }
1 Odpowiedź
+ 2
Because of type conversion. Adding number to char is converting the char to it's equivalent value in integer as defined by Ascii.
As to why number isn't converted to char , i don't have much knowledge of it but i assume it's because 1 being an integer is a bigger data type than char and so being converted to char can lead to loss of data while conversion of char to int won't result in any loss of data which is the default implicit conversion implemented by C#.
Take a look at this article,
https://www.google.com/url?sa=t&source=web&rct=j&url=https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions&ved=2ahUKEwi0o-3D7enxAhW0ILcAHcLXDdIQFjAKegQIDRAC&usg=AOvVaw2laqP_C3r-pmDPq1naA4hV&cshid=1626516503207