15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

namespaces ❘ 45<br />

}<br />

}<br />

{<br />

}<br />

case TimeOfDay.Morning:<br />

Console.WriteLine("Good morning!");<br />

break;<br />

case TimeOfDay.Afternoon:<br />

Console.WriteLine("Good afternoon!");<br />

break;<br />

case TimeOfDay.Evening:<br />

Console.WriteLine("Good evening!");<br />

break;<br />

default:<br />

Console.WriteLine("Hello!");<br />

break;<br />

The real power of enums in <strong>C#</strong> is that behind the scenes they are instantiated as structs derived from the<br />

base class, System.Enum. This means it is possible to call methods against them to perform some useful<br />

tasks. Note that because of the way the .<strong>NET</strong> Framework is implemented there is no performance loss<br />

associated with treating the enums syntactically as structs. In practice, after your code is compiled, enums<br />

will exist as primitive types, just like int <strong>and</strong> float.<br />

You can retrieve the string representation of an enum as in the following example, using the earlier<br />

TimeOfDay enum:<br />

TimeOfDay time = TimeOfDay.Afternoon;<br />

Console.WriteLine(time.ToString());<br />

This will return the string Afternoon.<br />

Alternatively, you can obtain an enum value from a string:<br />

TimeOfDay time2 = (TimeOfDay) Enum.Parse(typeof(TimeOfDay), "afternoon", true);<br />

Console.WriteLine((int)time2);<br />

This code snippet illustrates both obtaining an enum value from a string <strong>and</strong> converting to an integer.<br />

To convert from a string, you need to use the static Enum.Parse() method, which, as shown, takes three<br />

parameters. The first is the type of enum you want to consider. The syntax is the keyword typeof followed<br />

by the name of the enum class in brackets. (Chapter 7 explores the typeof operator in more detail.) The<br />

second parameter is the string to be converted, <strong>and</strong> the third parameter is a bool indicating whether you<br />

should ignore case when doing the conversion. Finally, note that Enum.Parse() actually returns an object<br />

reference — you need to explicitly convert this to the required enum type (this is an example of an unboxing<br />

operation). For the preceding code, this returns the value 1 as an object, corresponding to the enum value of<br />

TimeOfDay.Afternoon. On converting explicitly to an int, this produces the value 1 again.<br />

Other methods on System.Enum do things such as return the number of values in an enum definition or list<br />

the names of the values. Full details are in the MSDN documentation.<br />

namesPaCes<br />

As you saw earlier in this chapter, namespaces provide a way of organizing related classes <strong>and</strong> other types.<br />

Unlike a file or a component, a namespace is a logical rather than physical grouping. When you define a<br />

class in a <strong>C#</strong> file, you can include it within a namespace definition. Later, when you define another class<br />

that performs related work in another file, you can include it within the same namespace, creating a logical<br />

grouping that gives an indication to other developers using the classes how they are related <strong>and</strong> used:<br />

namespace CustomerPhoneBookApp<br />

{<br />

using System;<br />

public struct Subscriber<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!