What is the difference between:
namespace Library{
class File{
//code inside it
}
}
and:
namespace Library{
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.
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.