15.02.2015 Views

C# 4 and .NET 4

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

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

flow Control ❘ 37<br />

The output from this is:<br />

s1 is a string<br />

s2 is a string<br />

s1 is now another string<br />

s2 is now a string<br />

Changing the value of s1 has no effect on s2, contrary to what you’d expect with a reference type! What’s<br />

happening here is that when s1 is initialized with the value a string, a new string object is allocated on<br />

the heap. When s2 is initialized, the reference points to this same object, so s2 also has the value a string.<br />

However, when you now change the value of s1, instead of replacing the original value, a new object will<br />

be allocated on the heap for the new value. The s2 variable will still point to the original object, so its value<br />

is unchanged. Under the hood, this happens as a result of operator overloading, a topic that is explored in<br />

Chapter 7. In general, the string class has been implemented so that its semantics follow what you would<br />

normally intuitively expect for a string.<br />

String literals are enclosed in double quotation marks (”.”); if you attempt to enclose a string in single<br />

quotation marks, the compiler will take the value as a char, <strong>and</strong> throw an error. <strong>C#</strong> strings can contain<br />

the same Unicode <strong>and</strong> hexadecimal escape sequences as chars. Because these escape sequences start with<br />

a backslash, you can’t use this character unescaped in a string. Instead, you need to escape it with two<br />

backslashes (\\):<br />

string filepath = "C:\\ProCSharp\\First.cs";<br />

Even if you are confident that you can remember to do this all the time, typing all those double backslashes<br />

can prove annoying. Fortunately, <strong>C#</strong> gives you an alternative. You can prefix a string literal with the at<br />

character (@) <strong>and</strong> all the characters in it will be treated at face value; they won’t be interpreted as escape<br />

sequences:<br />

string filepath = @"C:\ProCSharp\First.cs";<br />

This even allows you to include line breaks in your string literals:<br />

string jabberwocky = @"'Twas brillig <strong>and</strong> the slithy toves<br />

Did gyre <strong>and</strong> gimble in the wabe.";<br />

Then the value of jabberwocky would be this:<br />

'Twas brillig <strong>and</strong> the slithy toves<br />

Did gyre <strong>and</strong> gimble in the wabe.<br />

floW ConTrol<br />

This section looks at the real nuts <strong>and</strong> bolts of the language: the statements that allow you to control the<br />

flow of your program rather than executing every line of code in the order it appears in the program.<br />

Conditional statements<br />

Conditional statements allow you to branch your code depending on whether certain conditions are met or<br />

on the value of an expression. <strong>C#</strong> has two constructs for branching code — the if statement, which allows<br />

you to test whether a specific condition is met, <strong>and</strong> the switch statement, which allows you to compare an<br />

expression with a number of different values.<br />

The if statement<br />

For conditional branching, <strong>C#</strong> inherits the C <strong>and</strong> C++ if.else construct. The syntax should be fairly<br />

intuitive for anyone who has done any programming with a procedural language:<br />

if (condition)<br />

statement(s)<br />

else<br />

statement(s)<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!