“This is the 14th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

This error is generated when:

Error CS0051: Inconsistent accessibility: parameter type XXX is less accessible than method XXX’ error CS0051: Inconsistent accessibility: Parameter type XXX is less accessible than method XXX

Let’s take a look at accessibility in C# :

Declared accessibility meaning
public Access is unrestricted.
protected Access is limited to the containing class or types derived from the containing class.
internal Access is limited to the current assembly.
protected internal Access is limited to the current assembly or to types derived from the containing class.
private Access is limited to include classes.
private protected Access is limited to the containing class or to types derived from the containing class in the current assembly. Available since C# 7.2.

You’ll probably know why:

The return type and each of the types referenced in the formal parameter list of a method must be at least as accessible as the method itself. Make sure the types used in method signatures are not accidentally private due to the omission of the public modifier. For more information, see Access Modifiers. The return types of a method and the types that reference parameters cannot be less accessible than the methods themselves. Note that the type is not private because you forgot to modify the type used in the function signature with public. (CLASS and struct members default to private)

To further illustrate:

class MyClass { struct User { string name; int age; bool gender; } public void Foo(User user) //error CS0051 { user.name = "miao"; user.age = 10; user.gender = true; }}Copy the code

Method Foo has a public accessibility, and a reference parameter user is of type user, which is a member of MyClass. The default accessibility is private, which is lower than the accessibility of method Foo.

There are only two ways to change this: make User more accessible to public (try internal) or make method Foo less accessible to private.

Error CS0122: ‘main.user. name’ is inaccessible due to its protection level:

user.name   = "miao"; //error CS0122
user.age    = 10;     //error CS0122
user.gender = true;   //error CS0122
Copy the code

Struct members are private by default, so they are not visible. One might ask, isn’t it the same object? Actually, it’s not. Because the user is not a member of MyClass, it is just a local variable.

Finally, two reference links are attached: MSDN accessibility level compiler error code explanation