2

只是出于好奇...

我发现使用 Char 类型会产生令人讨厌的副作用,所以我想知道为什么设计师选择了类似的行为。由于隐式转换,以下内容是有效的:

int x = 'A';

但是,以下内容让我有点困惑,因为容易导致误解:

int y = 'P' + 'Q';

既然一个字符串是由字符组成的,而字符串的“总和”会产生另一个字符串,为什么字符的“总和”应该给出不同的东西呢?

另一个更糟糕的:

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

所有这一切都是因为隐式转换。我想知道这样做有什么好处,而不是强迫用户明确地将字符“转换”为整数(反之亦然)。

也许是我的失明,但我看到了更多的缺点而不是好处。

顺便说一句,已经有一个与该主题相关的问题,但似乎没有给出正当理由,除了“他们决定如此”或“还不错”。

C# 中的隐式类型转换

非常感谢你。

4

2 回答 2

1

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.

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);

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();

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.

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. Allocate memory for the length of string 1 plus the length of string 2.
  2. Copy string one to the front of the newly allocated array.
  3. Copy string two to the back of the newly allocated array.
  4. Return the new string's instance.

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 + ";";

Maybe the compiler will optimize this code, but you cannot rely on him. In this case I would prefer a wrapper to StringBuilder, or at least String.Format that internally uses the 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.

于 2013-02-11T11:13:41.467 回答
0

尝试回答“为什么隐式提升到 32 位”部分:无论如何,CPU 在其寄存器中的 32 位或 64 位整数上运行。这意味着硬件会进行隐式提升。该语言尽量不抽象硬件太多。

也就是说,这可能很烦人。

于 2013-02-11T11:21:28.697 回答