什么是隐式转换字符为整数好处?整数、字符、好处、隐式

2023-09-06 16:16:47 作者:笑我孤陋

只是出于好奇...

我发现一个讨厌的副作用与char类型的工作,所以我想知道为什么设计师选择了一个类似的行为。 下面是因为隐式转换的有效的,:

I discovered a nasty side-effect working with the Char type, so I wondering why the designers chosen a similar behavior. The following is valid, because of the implicit conversion:

int x = 'A';

但是,下面是一个有点混乱给我,因为容易导致misinter pretations:

However, the following is a bit confusing to me, because leads easily to misinterpretations:

int y = 'P' + 'Q';

由于字符串是由字符和字符串的总和产生另一个字符串,为什么字符的总和应该给不同的东西?

Since a string is made up of character, and the "sum" of strings yields another string, why a "sum" of characters should give something different?

另一种更糟糕的:

string s1 = 'H' + "ello"; //yields "Hello"
string s2 = 'H' + 'e' + "llo";  //yields "173llo"

所有这一切,因为隐式转换。 我想知道这样做有什么,而不是强迫用户显式投字为一个整数(如反之亦然需要)的好处。

All that because the implicit conversion. I'd like to know what are the benefits of doing so, instead of forcing the user to explicitly "cast" the character to an integer (as the vice-versa requires).

也许我失明,但我看到越来越多的缺点了好处。

Perhaps my blindness, but I'm seeing more disadvantages over benefits.

顺便说一句,已经有与此主题有关的问题,但似乎没有给一个正当的理由,不是他们决定让或不那么糟糕。其他

By the way, there is already a question related to this topic, but seems that none gave a valid reason, other than "they decided so" or "isn't so bad".

隐式类型转换在C#中

感谢你这么多提前。

推荐答案

Basicly我看到这里一个好处,我不相信的,这是一个糟糕的设计决策。在内部的字符是整数,要么。一个数字和字符之间的转换,基于等价表被称为编码

Basicly I see one benefit in here, and I do not belive that it is a bad design decision. Internally characters are integer numbers, either. The conversion between a number and an character, based on equivalence tables is called encoding.

然而,在.NET字符和字符串是单code连接codeD 。在UNI code中的前128个字符等于原 ASCII编码。

However in .NET characters and strings are unicode encoded. The first 128 characters in unicode are equal to the former ASCII encoding.

。想象一下,是这样的:

If you want to convert a string into a number (or back) this can be done pretty easy when you assume that characters are also numbers. Imagine something like this:

char c = '1';
int i = Convert.ToInt32(c);

数字字符和数字再presentation之间的偏移八方通的0x30 。内部,它现在可以这样写:

The offset between numerical characters and numerical representation is allways 0x30. Internally it is now possible to write something like this:

int result = c - 0x30;

if (result < 0 || result > 9)
    throw new InvalidCastException();

请注意,该示例适用于人物,因为1个字符只能容纳1数值文字(0〜9)。对于字符串还需要倍增字符的用10和的结果的索引,并把它添加到总的结果值

Note that the example works for characters since 1 character can only hold 1 numerical literal (0 to 9). For strings you also need to multiply the index of the character with 10 and the result and add it to the overall result value.

当然,这是很多喜欢它的工作原理引擎盖下。但实际上它是坏的设计,使用操作符+ (或减)用于字符串或字符。这也有另外一个原因。想象一下以下code:

Of course this is much like it works "under the hood". But for practice it is bad design to use the operator+ (or minus) for strings or characters. This also has another reason. Imagine the following code:

string s1 = "Hello";
string s2 = " ";
string s3 = "World";

string helloWorld = s1 + s2 + s3;

在调用操作符+ 下面是发生了什么:

When calling the operator+ the following is happening:

分配内存串1的长度加上字符串2的长度。 复制串的一个新分配阵列的前部。 复制串两到新分配的阵列的后面。 返回新字符串的实例。

这会发生两次,所以你将有一个临时的字符串实例,强调垃圾收集器。由于这个例子是pretty的简单,它可能不会太大,但我也发现了类似这样的许多code样品字符串:

This will happen two times, so you will have a temporary string instance, stressing the garbage collector. Since this example is pretty simple it might not be much, but I also found strings like this many code samples:

string sql = "SELECT " + c1 + ", " + c2 + 
    " FROM " + table + 
    " WHERE " + c1 + " = " + condition + ";";

也许编译器将优化这个code,但你不能依赖他。在这种情况下,我会preFER一个包装的 StringBuilder的 ,或至少的的String.Format 在内部使用了 StringBuilder的

要得出一个结论: 从字符到整数隐式转换是有用的,以简化编码,但你不应该使用加减运算,当它来到的建立字符串或字符。

To come to a conclusion: Implicit conversion from characters to integers are usefull to simplify encoding, but you should not use plus or minus operators when it come's to build up strings or characters.