03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Exercises ❘ 315<br />

private ConsoleTyes Console { get; set; }<br />

}<br />

// Display the Person's name.<br />

public void ShowName()<br />

{<br />

Console.WriteLine(Name);<br />

}<br />

The intent is for the Console property to store the player’s console type. Unfortunately, when the<br />

compiler sees Console in the ShowName method, it finds the property, not the class, that displays<br />

messages in the Console window.<br />

If you place global:: in front of a namespace, the compiler begins searching for the symbol in the<br />

global namespace instead of looking for it locally. That means you can use the following statement<br />

to correctly find the Console.WriteLine method.<br />

global::System.Console.WriteLine(Name);<br />

Usually it is better to avoid names that conflict with the system namespaces. For example, if you<br />

change the property’s name to ConsoleType, there’s no confusion.<br />

Summary<br />

Namespaces are everywhere in <strong>C#</strong>. Every piece of code you write is contained in some namespace,<br />

even if it is only the application’s root namespace. Despite the pervasiveness of namespaces, many<br />

developers never need to use them explicitly, so they find them somewhat mystifying.<br />

Namespaces are quite simple. They merely divide programming items into a hierarchy to prevent name<br />

collisions, and they enable you to group related items.<br />

The using directive lets a program refer to items in a namespace without using fully qualified<br />

names. The using directive can also define an alias for a namespace, so you can refer to it by using<br />

a short abbreviation. This is particularly useful for resolving names that appear in more than one<br />

of the namespaces that your program uses.<br />

The .NET Framework contains hundreds of namespaces, some of which are more useful than<br />

others. The next chapter describes classes that are in two of the most useful namespaces: System<br />

.Collections and System.Collections.Generic. The classes in those namespaces let you arrange<br />

and manage objects in particularly useful ways such as in stacks, queues, lists, and dictionaries.<br />

Exercises<br />

1. Suppose you are writing a program that uses the System.Security.Cryptography<br />

.SHA512Managed class but you (understandably) don’t want to type all that out. Give two<br />

methods for shortening this in your code. What are the advantages and disadvantages of each<br />

2. Suppose your program needs to use the System.Windows.Controls.Calendar control<br />

and the System.Globalization.Calendar class. Give three methods for differentiating<br />

between the two classes in your code. What are the advantages and disadvantages of each<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!