开发者

Invalid argument in StreamWriter constructor?

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 argument开发者_如何学运维s" ... "cannot convert from 'System.Text.Encoding' to 'bool'" on the second line.

Sorry, not a question, rather a mistake.


Because filename is a string (apparently) and can not be converted to a Stream. 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.


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 is string, as I suppose, not Stream, so the second constructor is more preferable than the first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜