C
C#14mo 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
Thinker14mo ago
Well, simplified yes, Program here is public. Types in namespaces cannot actually be private, because they wouldn't be private to anything.
CrazieNewb
CrazieNewbOP14mo ago
actually i just tested and all classes are defaulted to public ;p, i just assumed its private because attributes in classes are.
Thinker
Thinker14mo 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
CrazieNewbOP14mo 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
Thinker14mo 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
CrazieNewbOP14mo ago
that seems like you want internal to be default, unless your explicitly coding an api or library
Thinker
Thinker14mo ago
yeah Although in practice you should always specify the accessibility to make things more readable.
InsertPi
InsertPi14mo 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
CrazieNewbOP14mo ago
exactly what i mean, you never want your classes to be used unless its specifically meant to be
InsertPi
InsertPi14mo 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
CrazieNewbOP14mo ago
it feels like it should be defaulted to internal, and a keyword should be used to make it accesable
Thinker
Thinker14mo 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
CrazieNewbOP14mo ago
oh it is?
Thinker
Thinker14mo ago
yes default is the lowest accessibility
CrazieNewb
CrazieNewbOP14mo ago
ah well im dumb
InsertPi
InsertPi14mo 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
CrazieNewbOP14mo 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
InsertPi14mo ago
i have no idea whether it’s good or bad practice, but it helps me so i do it
CrazieNewb
CrazieNewbOP14mo ago
in the end thats exactly what scopes are for readability and organisation
Accord
Accord14mo 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.
Want results from more Discord servers?
Add your server