返回类型比方法更难访问

pro*_*007 42 .net c#

我是新手c#,这是我正在努力获得一些经验的个人项目的摘录.

当调用getRecipe()此类之外的函数时,我出现以下错误.我要保持我List私人的CookBook类,但仍然能够获得一个参考的一个RecipesList.我不想List公开.

任何意见是极大的赞赏!谢谢


错误

return type 'cookbook.Recipe is less accessible than method 'cookbook.CookBook.getRecipe(string)'
Run Code Online (Sandbox Code Playgroud)
public class CookBook
{
    private List<Recipe> listOfRecipes = new List<Recipe> {};
    public Recipe getRecipe(string name)
    {
        int i = 0;
        while (listOfRecipes[i].getRecipeName() != name)
        {
            i++;
        }
        return listOfRecipes[i];
    }
}
Run Code Online (Sandbox Code Playgroud)

Cha*_*thJ 140

Recipe课程公开.

  • @ prolink007:默认情况下,类是`internal`,而不是`private`.实际上,只有嵌套类可以是`private`. (9认同)
  • 我忘了c#中的类默认是私有的...谢谢. (2认同)

Jes*_*mos 9

您的Recipe类比方法更难访问.您应该检查Recipe不是私有/内部的,并且您可以从该类范围之外看到Recipe类(快速修复声明Recipe是公共类).

正如Michael Stum在下面的评论中指出的那样,没有访问修饰符的类默认为内部或私有(如果它是嵌套类).这可能是您的问题,而您可能刚刚宣布class Recipe而不是public class Recipe