C
C#8mo ago
CrazieNewb

❔ 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?
namespace HelloWorld {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello, World!");
}
}
}
namespace HelloWorld {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello, World!");
}
}
}
21 Replies
Thinker
Thinker8mo ago
Well, simplified yes, Program here is public. Types in namespaces cannot actually be private, because they wouldn't be private to anything.
CrazieNewb
CrazieNewb8mo ago
actually i just tested and all classes are defaulted to public ;p, i just assumed its private because attributes in classes are.
Thinker
Thinker8mo ago
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.
CrazieNewb
CrazieNewb8mo ago
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?
Thinker
Thinker8mo ago
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.
CrazieNewb
CrazieNewb8mo ago
that seems like you want internal to be default, unless your explicitly coding an api or library
Thinker
Thinker8mo ago
yeah Although in practice you should always specify the accessibility to make things more readable.
InsertPi
InsertPi8mo ago
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
CrazieNewb
CrazieNewb8mo ago
exactly what i mean, you never want your classes to be used unless its specifically meant to be
InsertPi
InsertPi8mo ago
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%
CrazieNewb
CrazieNewb8mo ago
it feels like it should be defaulted to internal, and a keyword should be used to make it accesable
Thinker
Thinker8mo ago
I mean, that's exactly how it is internal is the default, and you have to explicitly specify that something should be public
CrazieNewb
CrazieNewb8mo ago
oh it is?
Thinker
Thinker8mo ago
yes default is the lowest accessibility
CrazieNewb
CrazieNewb8mo ago
ah well im dumb
InsertPi
InsertPi8mo ago
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
CrazieNewb
CrazieNewb8mo ago
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
InsertPi
InsertPi8mo ago
i have no idea whether it’s good or bad practice, but it helps me so i do it
CrazieNewb
CrazieNewb8mo ago
in the end thats exactly what scopes are for readability and organisation
Accord
Accord8mo ago
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.