![](/img/trans.png)
[英]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.