Class Organization Through NamespacesWhat is a Namespace? With the introduction of Microsoft's new computing platform, commonly called .Net, you'll find that there are many new terms and acronyms defined and Namespace is one of them. In layman's term, a namespace defines the scope of a class. You may start to ask, why do we need another term to define things? Doesn't this new term create more confusion? To appreciate the problem that namespaces try to address, let's for a while go back to our little C++ world. To capitalise and get maximum investment on existing software, code must be highly reusable. Hence the idea of software components came about. It was a dream that one day, we can pull software together and we'll have the computer perform a task. All without writing a single line of code. It was a nice dream until we came across a brick wall. It is easy to define a class called Employee and a method Salary to go with it. If you're in a single organisation then control of how the Employee class is defined is easy. However if you are a software provider, this may cause naming conflicts with your clients because they may have already defined an Employee class, which is totally different to yours. To make things worse, if an application inherits Employee class, which one should it use? Namespaces tries to address this dilemma. By being able to define the scope of a class, developers can define their own implementation of Employee class independent of each other. The application can also decide which class to inherit. Therefore co-existence of two implementations the same class is now possible in C#. Now comes the notation. Part of an expressive language is the ability to naturally
express grammar and syntax of a construct. In C#, namespaces are accessed through the dot (.) operator. In the past articles, we have made extensive use of the System.Console.WriteLine() function. What we are trying to do here is to access the
WriteLine() function within the Console class, which is part of the System class. This type of notation obviously presents a problem if the class hierarchy is too deep. C# developers can define a shortcut in their code through the use of the using keyword. Listing 1 is an example of how this shortcut can be applied.
Listing 1 Excited? You're probably eager to find out how to define your own namespace. As a
The copyright of the article Class Organization Through Namespaces in C# Programming is owned by Jose Aniceto. Permission to republish Class Organization Through Namespaces in print or online must be granted by the author in writing.
Go To Page: 1 2 Articles in this Topic Discussions in this Topic |