❔ Classes in a namespace
I understand that a class has to be public in a namespace to work. However i understand that default scope is set to private? so in this code is Program public because it is in a namespace?
21 Replies
Well, simplified yes,
Program
here is public.
Types in namespaces cannot actually be private
, because they wouldn't be private to anything.actually i just tested and all classes are defaulted to public ;p, i just assumed its private because attributes in classes are.
Btw things in classes are called 'members', attributes are another thing
Both types and members in types default to the lowest possible accessibility. For members that is
private
, and for types in namespaces that is internal
- which is essentially the same thing as public
.i was tryan use the write term, i normally call it variable ;p
i dont understand internal, vs defaults the template to make it internal
as i read online, it said it limits the scope to the "assembly"? which is the whole application?
pretty much
C# applications consist of one or more projects, each of which contains multiple classes. If a class (or anything else) is
internal
then only classes inside the same project can access it.that seems like you want internal to be default, unless your explicitly coding an api or library
yeah
Although in practice you should always specify the accessibility to make things more readable.
im writing a library and i use internal a ton
if a class in my library is internal, other classes in my library can use it, but it’s invisible to users of my library
exactly what i mean, you never want your classes to be used unless its specifically meant to be
so like if i have some data structure that i need to implement, i might use internal for that because the user probably won’t need to know it exists
100%
it feels like it should be defaulted to internal, and a keyword should be used to make it accesable
I mean, that's exactly how it is
internal is the default, and you have to explicitly specify that something should be public
oh it is?
yes
default is the lowest accessibility
ah well im dumb
as a rule i dont use default accessibility modifiers for anything. i always explicitly declare them because it’s easier for me to glance at a definition and know exactly where and how it can be used
vsc auto generated the file with my main Program class being set to internal manually.
so i assumed its not default
that what vsc did i guess, i should adopt that too
i have no idea whether it’s good or bad practice, but it helps me so i do it
in the end thats exactly what scopes are for
readability and organisation
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.