0

I'm trying to understand C# namespaces. Suppose two namespaces exist with the same name and they have identical contents, i.e. all classes and methods also have identical names. How can the compiler distinguish between the two if I try to call a method, e.g. MyMethod in class MyClass in namespace MyNamespace? It's not likely a real-world scenario, but I find the information useful for understanding purposes.

Second question: When I dot on a namespace, e.g. System, I get a set of related namespaces like System.Configuration etc., but when I go to Microsoft's documentation I cannot find any namespace named Configuration contained in System namespace. Why is that? I can of course find the namespace if I look for System.Configuration, but I don't understand why the System.Configuration namespace is not nested inside the System namespace.

SimpleJack
  • 13
  • 1
  • #1: try it and see what happens; #2: the linked page is to the System namespace - [back up a step in the bread crumb](https://learn.microsoft.com/en-us/dotnet/api/?view=net-6.0) to explore namespaces. – Andrew S Mar 06 '22 at 22:00
  • Can you show example code for the first scenario? I don't understand what you ask about. – Chayim Friedman Mar 06 '22 at 22:01

1 Answers1

1

If you have two types that have the same name and same number of type parameters, and exist in the same namespace, then the assemblies that they are in must be different.

If your project references the assembly that one type is in, but not the other, then you will obviously only be able to refer to the type that is from the assembly that you referenced. If your project references both assemblies, then there will be a compiler error, like this. To fix it, you can use an extern alias.

Not being able to find System.Configuration in this page is merely due to how Microsoft organises their documentation. They have decided to list all the namespaces out here, rather than display a hierarchy. Note that System.Configuration is part of the .NET Platform Extensions, not plain old .NET, so you have to choose that in the dropdown.

Sweeper
  • 213,210
  • 22
  • 193
  • 313