In c# why (char)(1) + (char)(2) results in int 3
.net, c#, char, vb.net
Solution
In C# `char` is a 16-bit numeric type, so `+` means addition, not concatenation. Therefore, when you add `a` and `b` you get `a+b`. Moreover, the result of this addition is an `int` (see a quick demo).
If by "adding two characters" you mean "concatenation", converting them to a strings before applying operator `+` would be one option. Another option would be using `string.Format`, like this:
string res = string.Format("{0}{1}", charA, charB);
Problem
I am trying to covert some VB.NET code to C# and found this interesting thing. Adding two chars returns different results in VB.NET and C#. VB.NET - returns string ``` Chr(1) & Chr(2) = " " ``` C# - returns int ``` (char)(1) + char(2) = 3 ``` How can i add(concatenate) two chars in C#?