Posts Tagged ‘ Namespaces ’


I am still at the very beginning with my attempt to learn C# and ultimately writing apps for Windows Phone 7. That’s the reason why I have to learn so much more. And by writing stuff down, I can memorize it better.

So, if any of my readers think, that is boring what I am writing about, then you probably already know way more than I do. For those who don’t think this is boring stuff: You might be a beginner as well, or – in the best case – you like the way I am writing. Anyway, if you are a beginner, then you might want to write down everything new you learn.

Having said that, I happen to see the word “namespace” in each and every project in Visual Studio, so I thought it might make sense to have a closer look.

A namespace is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols. An identifier defined in a namespace is associated with that namespace. The same identifier can be independently defined in multiple namespaces. That is, the meaning associated with an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace.

For example, Tom works for company X and his employee ID is 123. Jane works for company Y and her employee ID is also 123. The reason Bill and Jane can be identified by the same ID number is because they work for different companies. The different companies in this case would symbolize different namespaces. There would be serious confusion if the two people worked for the same company, and still had the same employee ID.

Data storage devices and many modern programming languages support namespaces. Storage devices use directories (or folders) as namespaces. This allows two files with the same name to be stored on the device as long as they are stored in different directories.

In the .NET Framework Base Class Library all the classes are organized into namespaces. You might want to look at it as a last name for classes. For example, my first name is “Andrea” and there are thousands more with that name. So with adding a namespace, my last name, I can specify that name.

Let’s have a look at some code:

The class I want to create a new instance of is IsolatedStorageSettings. Everything before that are the namespaces. As you can see they are embedded. The “System” namespace has an “IO” child namespace, which again has an “IsolatedStorage” child namespace. And that “owns” my class, the “IsolatedStorageSettings”.

Many of the classes and namespaces in the .NET Framework Class Library belong to “System”. The “IO” namespace contains namespaces that deal with in/out, or rather getting data into or out of an application. The “IsolatedStorage” namespace further separates out those classes that deal with isolated storage which allows me to save data to the phone’s flash memory.

Now, my line of code is so long, it doesn’t even fit in one line if I want to have a look at it all at the same time. Either I have to scroll through it, or as I did here, I separate it into two lines.

To shorten the amount of code that I have to write, I can rely on the “using” statement. A “using” statement provides access to all namespaces and classes that belong to it. So I don’t have to type all of that out. I am telling the compiler: Hey, I am using those namespaces, so before you complain, go, check the namespaces first to ensure that the class that I am referencing in my code belongs to one of them. If you don’t see the class in of of the namespaces, then go ahead and complain, and I’ll fix the problem.

In my little example there are a series of using statements right in the beginning of my MainPage.xaml.cs file. The new xaml page template adds them by default whenever I create a new xaml page:

Very convenient, so I don’t have to add them myself.

Some of them are used immediately, some are not.

I could remove using statements, that I know I won’t need or use in my application, or I could add more to it. Removing them is not necessary, because unused using statements are just ignored by the compiler.

Going back to my example: I commented out that immense line of code, and typed in this little line:

By using the key combination “Ctrl” and period a window pops up, offering me the using statement. Selecting that, the color changes from black to blue, indicating that I am working with a class name. Also what happens is that Visual Studio adds that using statement to that series of using statements I pointed out before:

That simple trick cuts my long line of code almost in half:

So far for now.

To be continued…




%d bloggers like this: