What is the difference between:
namespace Library{
class File{
//code inside it
}
}
and:
namespace Library{
public class File{
//code inside it
}
}
So what will be the difference between public class and class?
What is the difference between:
namespace Library{
class File{
//code inside it
}
}
and:
namespace Library{
public class File{
//code inside it
}
}
So what will be the difference between public class and class?
By default, all
class
es (and all types for that matter) areinternal
, so in order for them to be accessible from the outside (sans stuff likeInternalsVisibleToAttribute
) you have to make thempublic
explicitly.Without specifying
public
the class is implicitlyinternal
. This means that the class is only visible inside the same assembly. When you specifypublic
, the class is visible outside the assembly.It is also allowed to specify the
internal
modifier explicitly:The former is equivalent to:
All visibilities default to the least visible possible -
private
for members ofclass
es andstruct
s (methods, properties, fields, nested classes and nestedenum
s) andinternal
for direct members ofnamespace
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.