Wednesday, October 1, 2008

Namespaces are tool boxes

So, I was trying to think of things that I really just didn’t “get” when I recently started learning C# and namespaces, classes and methods came to mind.

I’ve been using computers all of my life but my programming days ended after my Commodore 64 power supply bit the dust. I have very fond memories of reading this book late into the evening when I was a kid. Peek or poke anyone?

Now, I’ve done a lot of scripting over the years both on *NIX and Windows but this C# stuff felt really new to me and kinda alien at first.

One of the first things that I got hung up on is the concept of namespaces. namespaces are something every application you write will use. Most will use more than one namespace. I like to think of them as tool boxes. Perhaps you can come up with a better metaphor, but I can’t. So, they are tool boxes for now.

Now an empty tool box isn’t much of a toolbox, so you need to fill it.

Microsoft gives you a lot of full tool boxes to get started with in the .NET Framework. In fact, you can see them all in this cool poster.

Now, tool boxes need tools. The tools that namespaces contain are called classes. Classes, like actual tools, are manipulated by methods.

Like this:

toolbox.screwdriver.turn screw;

namespace name.class name.method name;

and the real code would look like this:

System.Console.WriteLine ("Hello World");

System is the tool box, Console is the screwdriver (you know, that DOS prompt thing) and WriteLine is the method or action

So this bit of code opens the System tool box, pulls out the Console tool and Writes a Line (Hello World!)

This may seem over simplified to some of you, but it really helped me understand namespaces, classes and methods.

You can also create your own namespaces and fill them with your own classes and methods.

We'll save that for another post.

Am I way off the mark here?

How would you describe namespaces, classes and methods?

-------

I found this article helpful.

You can find a high res version on the .NET Framework poster here.




No comments: