C# 不一致的可访问性 : return type is less accessible than method

标签 c# .net access-modifiers

我正在为我的学习开发一个应用程序。现在我刚刚启动了一个应用程序,在那里我得到了一个包含足球联赛和俱乐部等的数据库。现在我有一个俱乐部和球员的名单,现在我试图添加更多的联赛,然后只有 1 个。但是当我收到这个错误时做同样的事情然后做之前。 这是无效列表的代码:

public List<Competitie> GetAllCompetities()
        {
            List<Competitie> Competitie = new List<Competitie>();
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                string query = "Select * from competitie";
                MySqlCommand selectallcompetitiecommand = new MySqlCommand(query, connection);
                MySqlDataReader reader = selectallcompetitiecommand.ExecuteReader();
                while (reader.Read())
                {
                    Competitie comp = new Competitie();
                    comp.IdCompetitie = reader.GetInt32(0);
                    comp.NaamCompetitie = reader.GetString(1);
                    Competitie.Add(comp);
                }
            }
            return Competitie;
        }

然后这是正在执行此操作的俱乐部的代码:

public List<Clubs> GetAllClubs(string selecteditem)
        { //Zorgt voor alle dingen van de tabel clubs.
            List<Clubs> Clubs = new List<Clubs>();
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {  
                connection.Open();
                string query = "Select * from databasevoetbal.clubs where competitie.naamcompetie = '" + selecteditem + "' and clubs.idcompetitie = competitie.idcompetitie";
                MySqlCommand selectAllClubsCommand = new MySqlCommand(query, connection);
                MySqlDataReader reader = selectAllClubsCommand.ExecuteReader();
                while (reader.Read())
                {
                    Clubs Club = new Clubs();
                    Club.idClubs = reader.GetInt32(0);
                    Club.NaamClubs = reader.GetString(1);
                    Club.aantalkampioenschappen = reader.GetInt32(2);
                    Club.opgericht = reader.GetInt32(3);
                    Club.idcompetitie = reader.GetInt32(4);
                    Clubs.Add(Club);
                }
            }
            return Clubs;
        }

这是相同的代码,只是俱乐部中的查询使用列表框中的选定项目,但任何人都知道为什么我在第一个列表中出现此错误:

Error CS0050 Inconsistent accessibility: return type 'List<Competitie>' is less accessible than method 'DatabaseManager.GetAllCompetities()'

类(class)代码:

class Competitie
    {
        public int IdCompetitie { get; set; }
        public string NaamCompetitie { get; set; }

        public override string ToString()
        {
            return string.Format("{0}", NaamCompetitie);
        }
    } 

最佳答案

你必须公开你的类(class):

public class Competitie

如果您不指定访问修饰符,它默认为 internal(即只能在它编译到的程序集中访问)。

如错误所述,该类必须至少与返回它的方法一样可访问。

按照您现在的理解方式,可以调用 GetAllCompetities() 方法(因为它是公共(public)的)的代码有可能无法访问该方法返回的类。显然这不是合乎逻辑的情况 - 调用代码将无法使用或理解它返回的数据。

注意根据上下文,将 GetAllCompetities() 方法标记为 internal 以匹配类实际上可能更合适。这样,在程序集之外就无法访问它们。不过,这完全取决于您的情况和您的需要。我只是为了完整性而指出这一点。它会解决错误,但会导致这些代码段的可访问性级别不同。

这是 C# 访问修饰符的文档:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers

关于C# 不一致的可访问性 : return type is less accessible than method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53761584/

相关文章:

c# - 哪个是 delphi "class of "(类的类型)的 C# 声明?

c# - 在 Windows 窗体中显示控件集合

c# - 如何检查 bitArray 包含任何真值或任何假值?

c# - 如何检查列表中是否包含 NEST 中的术语?

.net - String.Compare 与 OrdinalIgnoreCase 的奇怪行为

java - Java中getter的访问安全

c# - 公共(public)变量和私有(private)变量之间的区别

c# - 在带有代码隐藏的 ASP.NET 网站中,.cs 文件在什么时候编译?

c# - 继承自 Array 类

java - 为什么在编译时会出现重复的修饰符错误?