2

我有以下代码,但给出错误“无法将类型'string'隐式转换为'char []'”

char[] hTempFile = new char[300 + 1];
hTempFile ="";
4

5 回答 5

5

A char[] is different to a string. If you intend to be an empty array, then:

hTempFile = new char[0];

or perhaps simply (if you add a few null-checks):

hTempFile = null;

There is also .ToCharArray() on a string, but that seems overkill here.

Frankly, for a file-name, it sounds like you should actually be using string here.

于 2012-09-14T08:34:37.620 回答
2

它看起来像 C 风格的字符串初始化,在 C# 中最好避免对字符串使用 char 数组,而是使用字符串类。

string hTempFile = string.Empty;
于 2012-09-14T14:48:52.933 回答
0

You can use String.ToCharArray() to get array of char from string....If the string is empty like in your given example, the returned array is empty and has zero length....

hTempFile = "".ToCharArray();
于 2012-09-14T08:35:53.063 回答
0

看起来您想将 hTempFile 设置为空字符串,或者更具体地说,是空字符串的 C 字符串表示形式。如果是这种情况,您需要做的就是

hTempFile[0] = 0;

由于 C 字符串是以 null 结尾的,因此在数组的第一个 char 中放置一个 null 字节可以有效地清空字符串。

于 2012-09-14T15:04:00.077 回答
0

你想达到什么目标?您已经将 hTempFile 定义为 char[] 类型。

您不能将字符串值分配给 hTempFile 。

于 2012-09-14T08:37:05.377 回答