Under the Hood of C# Alias Types and Namespaces

Code helping deal with Input/Output needs.

C# must be able to locate your classes in order to compile and run your program.

You can either provide the full namespace to your class or let C# find it itself.

That’s what happened in exampleThree, we didn’t specify which namespace our String object was in.

So C# went looking and found one that broke our program.

If we always wanted to avoid namespace conflicts, every class reference would include the full namespace.

That’s how we fixed our problem in exampleFour.

We included the full namespace to the real String class.

But full namespaces are cumbersome to type, so generally full namespaces are only used if your program would break otherwise.

When namespaces aren’t used, how does C# go about trying to find a class?Finding ClassesC# applies a simple search pattern to locating a class.

It searches the current namespace and all imported namespaces for the class.

If it finds a matching class name that’s what it will resolve to.

Move to the parent namespace and start again at 1.

It repeats these steps until it finds a matching class or runs out of namespaces to search through.

Knowing this, lets revisit some of our previous examples.

We’ll annotate how C# decides to resolve namespaces for classes.

Example 2 RevisitedCompiler needs to locate where the String class is defined.

It looks through SolarSystem namespace and doesn’t find anything.

Moves up to the global namespace, the System namespace is imported and there is a String defined in System.

It chooses that string and everything works correctly.

Example 3 RevisitedCompiler needs to locate String.

It looks through SolarSystem namespace, finds a String class and uses that one.

Unfortunately that class is not the real String class, so a exception is thrown.

It found SolarSystem.

String before ever getting to the global namespace which is where System.

String is imported.

It stops as soon as it finds something even if that’s not what we wanted.

Example 4 RevisitedHere we provide the full namespace to String, C# doesn’t have to search for the class so everything works as we expect it to.

Example 6 RevisitedCompiler needs to locate String.

It looks through SolarSystem.

Earth namespace.

No String class is defined there but the System namespace is imported and it finds a String in that namespace.

Everything works!Like exampleFour, C# stops as soon as it finds a matching class name.

Since we changed where the using statement was we allowed C# to stop searching before finding SolarSystem.

String.

Wrapping UpAlias types and namespaces are something I use everyday.

For a considerable amount of time, I never really understood how they worked.

But now you know better and can impress all your C# friends!I’m Morgan Kenyon.

I’m a .

NET developer working in the DFW area.

I find C# a great language to use and it’s also backed by a great ecosystem, I love solving hard problems and want to continue talking about the tech I use.

If you found this article helpful or thought provoking leave a comment and lets connect over LinkedIn!.

. More details

Leave a Reply