C
C#2y ago
custard

❔ More details on the equivelent of modules in c#?

In Python and Javascipt, I can have modules and packages and must import them to access stuff within them. It seems the C# equivalent is namespaces. I think I get the basic idea. It seems that if I don't place something within a namespace, it is accessible everywhere within the project. Meanwhile, placing stuff within a namespace means the namespace itself is accessible everywhere. Is that all there is to the 'accessibility' of top-level names across files? If not, where can I find details? (I don't really know the specific terms to use, hope that was comprehensible)
10 Replies
Angius
Angius2y ago
Well, you can also have multiple projects And while namespaces might be available everywhere, classes from within them don't have to be Well, in case of using a separate project, at least
custard
custard2y ago
I see. Where can I find more details on how this kind of 'visibility' works? I manually tried out if a class was accessible from another file and stuff. I spent some times googling but haven't really found anything.
Angius
Angius2y ago
Access Modifiers - C# Programming Guide
All types and type members in C# have an accessibility level which controls whether they can be used from other code. Review this list of access modifiers.
custard
custard2y ago
thanks. not sure if it covers what i'm looking for. i mean nothing about accessing across files foe example
Angius
Angius2y ago
Files don't matter in C# Not in the slightest Namespaces do
Thinker
Thinker2y ago
The closest thing to 'modules' in C# are projects. A project is a single root directory containing a .csproj file. That project may then be published as a library and referenced through a Nuget package reference, or just reference through a relative file path. Namespaces and files literally do not matter in any sense of the word, they're nothing more than ways to conveniently structure and organize your classes. Namespaces are like folders on your computer, they do nothing more than organize files. You could just put everything on your desktop if you wanted to.
custard
custard2y ago
or just references through a relative file path
how would I do that? found something quite different on SO
Thinker
Thinker2y ago
<ProjectReference Include="Path/To/Project.csproj"/> Add that in your .csproj file, or you can add it in VS or Rider automatically.
custard
custard2y ago
Thanks both of you for the answers
Accord
Accord2y 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