我想要实现的是,显示以给定字符(通过 textbox1)结尾/以(用户通过 comboBox1 选择类型)开头的行数。
尝试编译此代码:
string needle=textBox1.Text.Trim(), cboxSelection = comboBox1.Text;
int count;
switch (cboxSelection)
{
case "Starting with":
count = File.ReadLines(openFileDialog1.FileName).Count(line => Regex.IsMatch(line, "^" + needle + ".*$"));
break;
case "Ending with":
count = File.ReadLines(openFileDialog1.FileName).Count(line => Regex.IsMatch(line, "^.*" + needle + ".*$"));
break;
}
string strCount = count.ToString(); // error line
label6.Text = "There are " + strCount + " lines " + cboxSelection + " " + needle + " character.";
收到错误消息:Use of unassigned local variable 'count'
。我错过了什么?