如何在C#中删除不一致的辅助功能 [英] How to remove inconsistent accessibility in C#
问题描述
创建列表方法
方法名称说:不一致的可访问性
我尝试过:
while create a list method
method name says : inconsistent accessibility
What I have tried:
public List<category> Addcat()//this is the error while write this code
{
string qry = "select * from category";
List<category> list = new List<category>();
con.Open();
{
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
category cat = new category();
cat.id = Convert.ToInt32(dr[0]);
cat.nam = dr[1].ToString();
}
dr.Close();
con.Close();
}
return list;
}
推荐答案
不要将代码作为解决方案发布 - 从未答复列表中删除您的问题并制作它不太可能被看到。我把你的代码移到了问题中,并删除了你的答案
不一致的可访问性意味着你试图添加一些声明比包含它的类。
例如,内部
类,其中public
方法:
Don't post code as a solution - that removes your question from the "unanswered" list and make it less likely to be looked at. I moved your code into the question, and deleted your "answer"
"Inconsistent accessibility" means that you are trying to add something which is declared as more available than the class that contains it.
For example, ainternal
class with apublic
method:
internal class MyClass
{
public void MyMethod (){}
}
因为该类是 internal
,所以它只能在同一个程序集中的文件中访问,在程序集之外它不能使用或甚至引用。
但是 MyMethod
是 public
,这意味着它可以被使用在任何地方。那么有谁可以使用它呢?他们可以访问 MyMethod
,但不能声明或使用 MyClass
类型的变量 - 这意味着 Mymethod
是一个 public
方法,不能在同一个程序集之外使用!
这就是错误消息所说的:不一致的可访问性:该方法的可访问性大于包含它的类的可访问性,因此无法按预期使用
将您的班级更改为 public
,或将您的方法更改为与班级匹配(或不太可访问)。
Because the class is internal
it is only accessible within files in the same assembly, outside the assembly it can't be used or even referenced at all.
But MyMethod
is public
, which means it can be used anywhere at all. So how can anyone use it? They can access MyMethod
, but not declare or use a variable of type MyClass
- which means Mymethod
is a public
method that can't be used outside the same assembly!
And that's what the error message is saying: "Inconsistent accessibility": "the accessibility of the method is greater than the accessibility of the class that contains it so it can't be used as you might expect"
Change your class to public
, or change your method to match (or be less accessible than) the class.
这篇关于如何在C#中删除不一致的辅助功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!