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.

26 ❘ ChaPTer 2 cOre c#<br />

All <strong>C#</strong> code must be contained within a class. The class declaration consists of the class keyword, followed<br />

by the class name <strong>and</strong> a pair of curly braces. All code associated with the class should be placed between<br />

these braces.<br />

Next, you declare a method called Main(). Every <strong>C#</strong> executable (such as console applications, Windows<br />

applications, <strong>and</strong> Windows services) must have an entry point — the Main() method (note the capital M):<br />

public static void Main()<br />

{<br />

The method is called when the program is started. This method must return either nothing (void) or an<br />

integer (int). Note the format of method definitions in <strong>C#</strong>:<br />

[modifiers] return_type MethodName([parameters])<br />

{<br />

// Method body. NB. This code block is pseudo-code.<br />

}<br />

Here, the first square brackets represent certain optional keywords. Modifiers are used to specify certain<br />

features of the method you are defining, such as where the method can be called from. In this case, you<br />

have two modifiers: public <strong>and</strong> static. The public modifier means that the method can be accessed from<br />

anywhere, so it can be called from outside your class. The static modifier indicates that the method does<br />

not operate on a specific instance of your class <strong>and</strong> therefore is called without first instantiating the class.<br />

This is important because you are creating an executable rather than a class library. You set the return type<br />

to void, <strong>and</strong> in the example, you don’t include any parameters.<br />

Finally, we come to the code statements themselves:<br />

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

Console.ReadLine();<br />

return;<br />

In this case, you simply call the WriteLine() method of the System.Console class to write a line of text to<br />

the console window. WriteLine() is a static method, so you don’t need to instantiate a Console object<br />

before calling it.<br />

Console.ReadLine() reads user input. Adding this line forces the application to wait for the carriage<br />

return key to be pressed before the application exits, <strong>and</strong>, in the case of Visual Studio 2010, the console<br />

window disappears.<br />

You then call return to exit from the method (also, because this is the Main() method, you exit the<br />

program as well). You specified void in your method header, so you don’t return any values.<br />

Now that you have had a taste of basic <strong>C#</strong> syntax, you are ready for more detail. Because it is virtually<br />

impossible to write any nontrivial program without variables, we will start by looking at variables in <strong>C#</strong>.<br />

Variables<br />

You declare variables in <strong>C#</strong> using the following syntax:<br />

datatype identifier;<br />

For example:<br />

int i;<br />

This statement declares an int named i. The compiler won’t actually let you use this variable in an<br />

expression until you have initialized it with a value.<br />

After it has been declared, you can assign a value to the variable using the assignment operator, =:<br />

i = 10;<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!