StreamWriter 构造函数中的参数无效?
在 C# 3.0 框架中与 3.0(程序集 mscorlib.dll,v2.0.50727) System.IO.StreamWriter
有以下构造函数:
public StreamWriter(Stream stream, Encoding encoding);
public StreamWriter(string path, bool append, Encoding encoding);
因此代码
Encoding enc = System.Text.Encoding.GetEncoding("iso-8859-1");
writer = new StreamWriter(filename, enc);
writer = new StreamWriter(filename, false, enc);
给出编译错误“最佳重载方法匹配...有一些无效参数”... 第二行是“无法从‘System.Text.Encoding’转换为‘bool’”。
抱歉,这不是一个问题,而是一个错误。
In C# 3.0, framework, vs. 3.0 (assembly mscorlib.dll, v2.0.50727)System.IO.StreamWriter
has a.o. the following constructors:
public StreamWriter(Stream stream, Encoding encoding);
public StreamWriter(string path, bool append, Encoding encoding);
So the code
Encoding enc = System.Text.Encoding.GetEncoding("iso-8859-1");
writer = new StreamWriter(filename, enc);
writer = new StreamWriter(filename, false, enc);
gives the compilation error "The best overloaded method match ... has some invalid arguments" ...
"cannot convert from 'System.Text.Encoding' to 'bool'" on the second line.
Sorry, not a question, rather a mistake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为文件名是一个
字符串
(显然)并且不能转换为Stream
。编译器假定第二个重载是您尝试使用的重载。它可能会假设说“无法将字符串转换为 Stream”,但编译器不会按该顺序解析参数。Because filename is a
string
(apparently) and can not be converted to aStream
. The compiler assumes the second overload is the one you try to use. It could hypothetically have said "can not convert string to Stream" but the compiler does not resolve arguments in that order.第二行需要不存在的构造函数 StreamWriter(string, Encoding) ,而不是存在的 StreamWriter(Stream, Encoding) 。
The second line would require the constructor StreamWriter(string, Encoding) which doesn't exist, not StreamWriter(Stream, Encoding) which exist.
我假设变量“文件名”是一个字符串,因此它首先匹配第二个构造函数,然后在将编码转换为布尔值的第二个参数上失败。
I assume variable "filename" is a string, so therefore it's matching the 2nd constructor first, then failing on the 2nd parameter converting an Encoding to a Boolean.
正如我所想,
filename
是字符串,而不是 Stream,因此第二个构造函数比第一个构造函数更可取。filename
is string, as I suppose, not Stream, so the second constructor is more preferable than the first.