c# - 帮助解决简单的 C# 错误

标签 c# .net

我正在编写这个小程序来从文本文件中提取任意数量的电子邮件地址。我收到两个错误,“使用未分配的局部变量。”我不确定为什么。

static void Main(string[] args)
{
string InputPath = @"C:\Temp\excel.txt";
string OutputPath = @"C:\Temp\emails.txt";
string EmailRegex = @"^(?:[a-zA-Z0-9_'^&/+-])+(?:\.(?:[a-zA-Z0-9_'^&/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$";
string Text = String.Empty;
StringBuilder Output;
List<string> Emails;

using (TextReader tr = new StreamReader(InputPath))
{
    Text = tr.ReadToEnd();
}

MatchCollection Matches = Regex.Matches(Text,EmailRegex);

foreach (Match m in Matches)
{
    Emails.Add(m.ToString().Trim()); // one error is here
}

foreach (String s in Emails)
{
    Output.Append(s + ","); // the other error is here
}

using (TextWriter tw = new StreamWriter(OutputPath))
{
    tw.Write(Output.ToString());
}
} 

抱歉格式化...我有点时间紧迫!

编辑:哇。我是个白痴 - 一定是因为我时间紧迫!!!!

最佳答案

您没有初始化 StringBuilder 和 List。

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<String>();

关于c# - 帮助解决简单的 C# 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1143147/

相关文章:

.net - 隐藏错误报告窗口

.net - 如何指示 NDepend 忽略 EF 类?

c# - 如何使用 C# 仅获取目录中的文件名?

c#如何使用图形路径制作平滑的圆弧区域

c# - 通过 cast 或 Convert.ToSingle() 将 double 转换为 float?

c# - 带文字的半透明圆形控件

c# - 通过二进制文件可以看到源代码吗? (C#)

c# - 无法在我的解决方案中添加来自新项目的新引用

.net - 是否可以在客户端凭证流程中为应用程序注册赋予角色?

c# - 不同网络平台/网络产品之间常见的同音异义词和同义词有哪些?