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

Create successful ePaper yourself

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

Literal Type Characters ❘ 69<br />

NOTE The syntax List creates a List object that manipulates<br />

strings. List is a generic class and in this example string is its generic parameter.<br />

Chapter 14, “Collection Classes,” says more about generic collection classes.<br />

The items inside the braces must include all the values needed by the collection’s Add method. For<br />

example, the Dictionary class’s Add method takes two parameters giving a key/value pair that should<br />

be added. That means each entry in the initializer should include a key and value.<br />

The following code initializes a Dictionary (dictionary with keys that are<br />

strings and associated values that are strings). The parameters to the class’s Add method are an<br />

item’s key and value so, for example, the value 940-283-1298 has the key Alice Artz. Later you could<br />

look up Alice’s phone number by searching the Dictionary for the item with the key "Alice Artz".<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 />

Initializing Without Add<br />

Some collection classes such as Stack and Queue don’t have an Add method, so this<br />

kind of initializer doesn’t work for them. Fortunately, they have constructors that<br />

take a parameter that can be an enumerable type such as a list or array. That means<br />

you can pass the constructor an array of values to be added to the Stack or Queue<br />

as in the following code.<br />

Stack stack = new Stack(new int[] { 1, 2, 3, 4 });<br />

Queue queue = new Queue(new int[] { 4, 3, 2, 1 });<br />

Literal Type Characters<br />

If your code includes a literal value such as a number, <strong>C#</strong> uses a set of rules to interpret the value.<br />

For example, the value 2000000000 fits in the int data type, so when a <strong>C#</strong> program sees that<br />

value, it assumes it is an int.<br />

In contrast, the value 3000000000 does not fit in the int data type, so the program assumes this<br />

value is a uint, which is big enough to hold the value. A uint cannot hold a negative value, however,<br />

so if the program contains the value –3000000000, <strong>C#</strong> makes it a long.<br />

When a value looks like an integer, the program tries to interpret it as the smallest integer data type<br />

at least as large as int (so it doesn’t consider byte, sbyte, short, or ushort).<br />

When a value includes a decimal point, the program assumes it is a double.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!