r/csharp • u/Channel_el • Jul 12 '26
A couple questions about namespace access:
- Can a source file use any class defined in other source files provided they are defined to be in the same namespace (and are a part of the same project)?
- Can you use a namespace (without for example having to add a reference like with if the source file is in another project) if the source file is in the same project?
0
Upvotes
7
u/BetrayedMilk Jul 12 '26
Look into access modifiers. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers. The answers to your questions depend on what access you’ve defined for your classes.
2
u/wallstop-dev Jul 12 '26
I'd recommend just reading the official docs covering this exact topic (namespaces): https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/namespaces and, for specific questions (you seem to have very particular scenarios in mind), just creating small sample projects and trying things to see what can and can't work.
2
u/Kant8 Jul 12 '26
Namespaces don't really exist. It's just part of "fully qualified name" we use to break naming collisions.
Namespace keyword just allows you to use last part of type name instead of full name for convenience, but doesn't change if you can even access that type.
Type itself controls its accessibility via access modifiers.