我在try catch块中的IEnumerable <t>中提到了什么返回类型?

时间:2017-12-21 05:38:12

标签: c#

在这里,我对我需要使用的返回类型感到困惑:

public IEnumerable<Employee> GetEmployee123()
{
    try
    {
         var x = from n in db.Employees select n;
         return x;
    }
    catch (Exception ex)
    {   
         Business_Dll.Model.Errorhandlecls
            .ExceptionLogging.SendErrorToText(ex);
    }
}

1 个答案:

答案 0 :(得分:1)

我通常避免从方法返回空值而更喜欢使用Enumerable.Empty。这样就可以避免空值检查并安全地将此方法与其他方法(例如LINQ)一起使用。

public IEnumerable<Employee> GetEmployee123()
{
    try
    {
         var x = from n in db.Employees select n;
         return x;
    }
    catch (Exception ex)
    {   
         Business_Dll.Model.Errorhandlecls
            .ExceptionLogging.SendErrorToText(ex);

         return Enumerable.Empty<Employee>();
    }
}