In object-oriented programming, a public class is any part of the program that accesses or updates its members using the member name to be accessed. This type of class is useful for information or methods that need to be available in any part of the program code. Class, Private, Programming terms.
In C#, a class by default has internal access (i.e. this class is only visible within the assembly). A public class will have access in the assembly it is part of and all other assemblies where it is referenced.
A public class will have access specifier "public" and its members can be accessed with in and out of the class in which they are specified. A class which doesnt have the public access specifier are called as non-public classes and its member can be accessed only within the class in which they are specified.
Without specifying public
the class is implicitly internal
. This means that the class is only visible inside the same assembly. When you specify public
, the class is visible outside the assembly.
It is also allowed to specify the internal
modifier explicitly:
internal class Foo {}
The former is equivalent to:
namespace Library{
internal class File{
//code inside it
}
}
All visibilities default to the least visible possible - private
for members of class
es and struct
s (methods, properties, fields, nested classes and nested enum
s) and internal
for direct members of namespace
s, because they can't be private.
internal
means other code in the same assembly can see it, but nothing else (barring friend assemblies and the use of reflection).
This makes sense for two reasons:
public
you could accidentally make something public that should be private or internal. If you accidentally make something not visible enough, you get an obvious compile error and fix it. If you accidentally make something too visible you introduce a flaw to your code that won't be flagged as an error, and which will be a breaking change to fix later.It's often considered better style to be explicit with your access modifiers, to be clearer in the code, just what is going on.
By default, all class
es (and all types for that matter) are internal
, so in order for them to be accessible from the outside (sans stuff like InternalsVisibleToAttribute
) you have to make them public
explicitly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With