单声道错误?实际使用变量时出现 CS0219 警告
MonoDevelop(2.10.8)正在报告:
JPGCorruptForm.cs(20,20): Warning CS0219: The variable `myStream' is assigned but its value is never used (CS0219) (JPGCorrupt)
对于此功能:
private void toolStripButtonChooseText_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = ".";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = false;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
Stop();
try
{
if ((myStream = openFileDialog.OpenFile()) != null)
{
_settings.TextFile = openFileDialog.FileName;
CurrentTextFile = _settings.TextFile;
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
这是我的第一个单声道测试项目,我不确定这种事情是否正常。这当然不是致命的,但可能会变得烦人。
MonoDevelop (2.10.8) is reporting:
JPGCorruptForm.cs(20,20): Warning CS0219: The variable `myStream' is assigned but its value is never used (CS0219) (JPGCorrupt)
For this function:
private void toolStripButtonChooseText_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = ".";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = false;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
Stop();
try
{
if ((myStream = openFileDialog.OpenFile()) != null)
{
_settings.TextFile = openFileDialog.FileName;
CurrentTextFile = _settings.TextFile;
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
This is my frist mono test project and I'm not sure if this kind of thing is normal. It certainly is not fatal, but could get annoying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您正在为变量分配一个值,但您实际上从未读取它。换句话说,您可以轻松删除它,只需将中间表达式更改为:
请注意,您现有的代码实际上并没有从变量中读取,即使您可能认为它在与 null 进行比较时也是如此。它更像是这样的:
听起来你可能应该使用它,如果没有别的办法关闭流......尽管我会尽可能晚地声明它,就像这样:
这是一个更简单的相同问题的示例,但方式:
请注意,对于警告级别 4(可能还有更低的警告级别),Microsoft C# 4 编译器也会识别该问题:
Well you're assigning a value to the variable, but you're never actually reading from it. In other words, you could easily remove it, just changing the middle expression to:
Note that your existing code doesn't actually read from the variable even though you might think it does in the comparison to null. It's more like this:
It sounds like you probably should be using it, to close the stream if nothing else... although I'd then declare it as late as possible, like this:
Here's a simpler example of the same problem, but the way:
Note that with warning level 4 (and possibly lower ones), the Microsoft C# 4 compiler picks up on it too: