Access modifiers in C#
Let's say I set the access modifier internal to the class, and the variable in this class is public. Question, will this variable have an access modifier internal (as a class access modifier) or public (as a variable access modifier)?
1 answers
The public
variable can be accessed from anywhere in the assembly where the internal
class is declared.
Of course, to do this, you still need to get an instance of the class.
Explanation
The internal
modifier of a class means that it will not be possible to use it from outside the assembly in which it is declared (that is, it is impossible to create a variable with this type in another assembly, call the constructor of this class, or inherit from it, as well as access it static members).
At the same time, inside the assembly in which the class is declared, you can not make a public
member of the public
class that returns an instance of the internal
class - the compiler will take care of this, and throw out CS0050
.
Ultimately, this means that there is no way to get an instance of the internal
class in other assemblies.
The public
modifier for a class member means that it can be accessed by anyone, but only through an instance of the class.
In the documentation, you can read: internal (C# Reference).