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.

Enumerated Type Declarations ❘ 739<br />

To initialize a collection class that provides an Add method, create a new instance of the class followed<br />

by initial values enclosed in braces, as in the following code.<br />

List pies = new List()<br />

{<br />

"Apple", "Banana", "Cherry", "Coconut Cream"<br />

};<br />

If the collection’s Add method takes more than one parameter, group parameters in brackets, as in<br />

the following example.<br />

Dictionary directory = new Dictionary()<br />

{<br />

{"Alice Artz", "940-283-1298"},<br />

{"Bill Bland", "940-237-3827"},<br />

{"Carla Careful", "940-237-1983"}<br />

};<br />

Using<br />

To make it easy to call an object’s Dispose method, you can declare the object inside a using<br />

statement. When the code reaches the end of the using block, the program automatically calls the<br />

object’s Dispose method.<br />

For example, the following code creates a Graphics variable named gr that is automatically disposed<br />

when the using block ends.<br />

using (Graphics gr = Graphics.FromImage(bm))<br />

{<br />

...<br />

}<br />

Enumerated Type Declarations<br />

The syntax for declaring an enumerated type is as follows.<br />

«attributes0» «accessibility» enum name<br />

«: type»<br />

{<br />

«attributes1» name1 «= value1»,<br />

«attributes2» name2 «= value2»,<br />

...<br />

}<br />

The pieces of this declaration are as follows.<br />

➤➤<br />

➤➤<br />

➤➤<br />

attributes0—Attributes that specify extra properties for the enumeration.<br />

accessibility—This determines which code can access the variable.<br />

name—The name you want to give the enumeration.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!