vb.net 在 c# 中为什么 (char)(1) + (char)(2) 结果为 int 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18335482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:44:13  来源:igfitidea点击:

In c# why (char)(1) + (char)(2) results in int 3

c#.netvb.netchar

提问by SarkarG

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 代码转换为 C# 并发现了这个有趣的事情。在 VB.NET 和 C# 中添加两个字符会返回不同的结果。

VB.NET- returns string

VB.NET-返回字符串

Chr(1) & Chr(2) = "  "

C#- returns int

C#-返回 int

(char)(1) + char(2) = 3

How can i add(concatenate) two chars in C#?

如何在 C# 中添加(连接)两个字符?

回答by dasblinkenlight

In C# charis a 16-bit numeric type, so +means addition, not concatenation. Therefore, when you add aand byou get a+b. Moreover, the result of this addition is an int(see a quick demo).

在 C# 中char是 16 位数字类型,因此+意味着加法,而不是串联。因此,当您添加ab获得a+b. 此外,此添加的结果是int参见快速演示)。

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.Format,像这样:

string res = string.Format("{0}{1}", charA, charB);

回答by xanatos

By adding to an empty string you can force the "conversion" of charto string... So

通过添加到一个空字符串,你可以强制“转换”charstring......所以

string res = "" + (char)65 + (char)66; // AB

(technically it isn't a conversion. The compiler knows that when you add to a stringit has to do some magic... If you try adding nullto a string, it consider the nullto be an empty string, if you try adding a stringit does a string.Concatand if you try adding anything else it does a .ToString()on the non-string member and then string.Concat)

(从技术上讲它不是转换。编译器知道当你添加到 astring它必须做一些魔术......如果你尝试添加null到一个字符串,它认为 thenull是一个空字符串,如果你尝试添加一个stringit做一个string.Concat,如果你尝试添加任何其他东西,它会.ToString()在非字符串成员上做一个,然后string.Concat)

回答by Metalogic

The best answer is in the comments so I want elevate it here to a proper answer. With full credit going to @Jeow Li Huan:

最好的答案在评论中,所以我想在这里将其提升为正确的答案。完全归功于@Jeow Li Huan:

string res = string.Concat(charA, charB);

回答by r.net

(char)(1) has an ascii value of 1 and (char)(2) ascii value of 2

(char)(1) 的 ascii 值为 1, (char)(2) 的 ascii 值为 2

so ascii value of 1 + 2 (i.e. (char)1 + (char)2 ) will be equal to 3.

所以 1 + 2 的 ascii 值(即 (char)1 + (char)2 )将等于 3。

if you do: "2" + "1" this will give you "21" (althou you should not use this to join strings, bad practice)

如果你这样做:“2”+“1”这会给你“21”(虽然你不应该用它来连接字符串,不好的做法)

if you do: '2' + '1' this will give you int value of 99 that is ascii value of 2 (which is 50) + ascii value of 1(which is 49).

如果你这样做: '2' + '1' 这会给你 99 的 int 值,即 ascii 值 2(即 50)+ ascii 值 1(即 49)。