Entirely possible that you might pick a name thats

Info icon This preview shows pages 39–41. Sign up to view the full content.

entirely possible that you might pick a name that’s already being used, but namespaces make this less of a problem. For example, there’s a Bold class in .NET, but if you happen not to be using part of the library it belongs to (WPF’s text services) you might well want to use the name Bold to mean something else in your own code. And since .NET’s own Bold type is hidden away in the System.Windows.Documents namespace, as long as you don’t add a using directive for that namespace you’re free to use the name Bold yourself to mean whatever you like. Even when there’s no ambiguity, namespaces help you find your way around the class library—related types tend to be grouped into one namespace, or a group of related namespaces. (For example, there are various namespaces starting with System.Web con- taining types used in ASP.NET web applications.) So rather than searching through thousands of types for what you need, you can browse through the namespaces—there are only a few hundred of those. You can see a complete list of .NET Framework class library namespa- ces, along with a short description of what each one is for, at .microsoft.com/library/ms229335 . Visual Studio adds four namespace directives to the Program.cs file in a new console project. The System namespace contains general-purpose services, including basic data types such as String , and various numeric types. It also contains the Console type our program uses to display its greeting and which provides other console-related services, such as reading keyboard input and choosing the color of your output text. The remaining three using directives aren’t used in our example. Visual Studio adds them to newly created projects because they are likely to be useful in many applications. The System.Collections.Generic namespace contains types for working with collec- tions of things, such as a list of numbers. The System.Linq namespace contains types used for LINQ, which provides convenient ways of processing collections of informa- tion in C#. And the System.Text namespace contains types useful for working with text. The using directives Visual Studio adds to a new C# file are there just to save you some typing. You are free to remove them if you happen not to be using those namespaces. And you can add more, of course. Namespaces and Types | 15
Image of page 39

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Removing Unwanted Using Directives There’s a quick way to remove unwanted using directives. If you right-click anywhere on your C# code, the context menu offers an Organize Usings item. This opens a submenu that includes a Remove Unused Usings item—this works out which using directives are surplus to requirements, and removes them. The submenu offers another option designed to appeal to those who like to keep their source code tidy—its Remove and Sort entry can remove unused using statements and then sort the rest into alpha- betical order. This menu is shown in Figure 2-2 .
Image of page 40
Image of page 41
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern