Microsoft 9GD00001 Computer Accessories User Manual


 
42 Microsoft Visual Studio 2010: A Beginner’s Guide
which you’ll see many times throughout this book. The Console application defined the
skeleton code class to have the name Program. In reality you can name the class anything
you want. Whatever names you choose should make sense for the purpose of the class.
For example, it makes sense for a class that works with customers to be named Customer
and only contain methods that help you work with customers. You wouldn’t add methods
for working directly with invoices, products, or anything other than customers because
that would make the code in your Customer class confusing. Classes are organized with
namespaces, which are discussed next.
The FirstProgram Namespace
A namespace helps make your class names unique and therefore unambiguous. They
are like adding a middle name and surname to your first name, which makes your whole
name more unique. A namespace name, however, precedes the class name, whereas your
middle name and surname follow your first or given name. A namespace also helps you
organize code and helps you find things in other programmers’ code. This organization
helps to build libraries of code where programmers have a better chance to find what
they need. The .NET platform has a huge class library that is organized into namespaces
and assemblies; this will become clearer the more you program. The main .NET
namespace is System, which has multiple sub-namespaces. For example, guess where
you can find .NET classes for working with data? Look in System.Data. Another quick
test: Where are .NET classes for working with networking protocols like TCP/IP, FTP, or
HTTP? Try System.Net.
Another benefit of namespaces is to differentiate between classes that have the same
name in different libraries. For example, what if you bought a third-party library that has a
Customer class? Think about what you would do to tell the difference between Customer
classes. The solution is namespaces, because if each Customer has its own namespace,
you can write code that specifies each Customer by its namespace. Always using
namespaces is widely considered to be a best practice.
The Program class in Listing 2-1 belongs to the FirstProgram namespace, repeated
here for your convenience (in C#):
namespace FirstProgram
{
// Program class omitted for brevity
}
You can put many classes inside of a namespace, where inside means within the
beginning and ending braces for a namespace.