【发布时间】:2014-04-11 09:01:57
【问题描述】:
我有一个 c# 函数,它从数据表中读取文件位置,并将包含所有文件 lcoations 的列表返回给调用方法。
在Catch 块中,我想返回一个带有 false 的空列表,以便调用方法可以取消它的操作。
但我无法编译我的 return 语句。
传入一个列表作为参考,并让函数返回一个布尔值true/false 会更好吗?
这是我正在尝试的代码:
public static List<string> getEmailAttachments(string emailID, System.Data.DataTable emails)
{
List<string> allAttachments;
//System.Data.DataTable oTbl = new DataTable();
try
{
System.Diagnostics.Debugger.Break();
var results = from myRow in emails.AsEnumerable()
where myRow.Field<string>("itemID") == emailID
select myRow;
System.Diagnostics.Debug.Print("attachments");
foreach (DataRow myRow in results)
{
System.Diagnostics.Debug.Print(myRow.Field<string>("attachmentsPath"));
allAttachments.Add(myRow.Field<string>("attachmentsPath"));
//DataTable dt = (DataTable)myRow["attachmentsPath"];
//DataTable oTbl = dt.Clone();
//DataRow[] orderRows = dt.Select("CustomerID = 2");
//foreach (DataRow dr in orderRows)
//{
// oTbl.ImportRow(dr);
//}
// myTable.ImportRow(dr);
//oTbl.Rows.Add(myRow);
//oTbl.ImportRow(myRow);
}
return allAttachments;
}
catch (Exception ex)
{
logBuilder("common.getEmailAttachments", "Exception", "", ex.Message, "");
return new List<string>emptyList(); // cannot compile
}
}
【问题讨论】:
-
附带问题 - 这段代码有哪些类型的异常?也许您可以在不涉及异常的情况下处理这些情况?
标签: c# .net return-value return-type