[英]Why am I getting the error Error 1 Inconsistent accessibility: return type?
[英]Why am I obtaining this inconsistent accessibility error on the return type of a method?
我正在研究.NET项目。 在DAO类中(使用ADO.NET,但这并不重要),我有这样的方法:
public static List<Inoltro> GetListaInoltriUltimiGiorni(int IdUor, string Protocollista, int NumeroGiorni, DBConnection config)
{
string query = PROT_INOLTRO.INOLTRI_ULTIMI_N_GIORNI_BY_UOR_AND_PROTOCOLLISTA;
List<Inoltro> result = new List<Inoltro>();
using (SqlConnection con = ArxeiaConnection.getARXEIAStringConnection(config.Tenant + "_" + config.Database))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
try
{
cmd.Parameters.AddWithValue("@IdUor", IdUor);
cmd.Parameters.AddWithValue("@Protocollista", Protocollista);
cmd.Parameters.AddWithValue("@NumeroGiorni", NumeroGiorni);
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
// Read advances to the next row.
while (reader.Read())
{
.........................................
.........................................
.........................................
}
}
}
}
catch (Exception ex)
{
con.Close();
throw ex;
}
}
}
return result;
}
如您所见,我正在创建并返回此对象:
List<Inoltro> result = new List<Inoltro>();
问题是Visual Studio在此方法签名上给我以下错误:
可访问性不一致:返回类型
List<Inoltro>
的访问性比方法MyClass.GetListaInoltriUltimiGiorni(....)
为什么? 我想念什么? 我该如何解决?
您的方法是public
但Inoltro
是internal
。 您不能具有公开internal
类型的public
方法。 将方法internal
方法或将类型public
。
在查看您的代码时,会想到一些注意事项。
首先, try catch
是不必要的,因为如果引发异常, using
会在连接上执行Close
。 消除try catch
。
其次,请不要再throw ex;
供您将来参考throw ex;
catch
。 说throw;
重新抛出异常。 区别是微妙的。 throw;
重新引发捕获的确切异常。 throw ex;
重新引发异常,但将堆栈跟踪重置为当前跟踪,而不是原始跟踪。
您唯一想做的事就是throw ex;
例如,当您故意要掩盖异常的来源时,例如,因为当前代码与调用方之间存在信任边界。 但是,在那种情况下,最好还是抛出一个新的通用异常。
第三,我不明白为什么您要花while
和一个if
来检查集合是否有行。 当然,如果集合为空,则Read
将返回false,因此永远不需要if
,对吧?
第四,这只是一个风格点; 通常我们会格式化
using (SqlConnection con = ArxeiaConnection.getARXEIAStringConnection(config.Tenant + "_" + config.Database))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
如
using (SqlConnection con = ArxeiaConnection.getARXEIAStringConnection(config.Tenant + "_" + config.Database))
using (SqlCommand cmd = new SqlCommand(query, con))
{
通常,只有一个using
的主体只是另一个using
,在这种情况下,不必要的花括号和缩进不会使代码更易于理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.