Professional C# 6 and .NET Core 1.0. Christian Nagel

Чтение книги онлайн.

Читать онлайн книгу Professional C# 6 and .NET Core 1.0 - Christian Nagel страница 18

Professional C# 6 and .NET Core 1.0 - Christian Nagel

Скачать книгу

the application from within Visual Studio by using Debug ➪ Start Without Debugging. This starts the app as shown in Figure 2.7.

Screenshot shows a command prompt window with message Hello World! Press any key to continue.

Figure 2.7

      NOTE Be sure to not start the app with Debug ➪ Start Debugging; if you do you will not see the output of the app because the console window immediately closes after the app completes. You can use this method to run the app either with setting breakpoints and debugging into the app, or by adding a ReadLine method before the end of the Main method.

You can use the Debug tab in the project properties to configure the runtime version that should be used while running the app (see Figure 2.8).

Screenshot shows a project configuration window for HelloWorldApp with highlighted Debug tab on the left side, fields for profile, launch, command et cetera along with Add, Browse, New and Remove buttons.

Figure 2.8

      TIP When you have multiple projects in the same solution, you can define what project should run by selecting the project in Solution Explorer and opening the context menu. In the context menu click Set as Startup Project (or Project ➪ Set as Startup Project). Alternatively, you can select the solution in the Solution Explorer, and select Set Startup Projects to open the property page for the solution where you can select what should be the startup project. You can also define multiple projects to start.

      Taking a Closer Look at the Code

      Now let’s concentrate on the C# source code. First, I have a few general comments about C# syntax. In C#, as in other C-style languages, statements end in a semicolon (;) and can continue over multiple lines without needing a continuation character. Statements can be joined into blocks using curly braces ({}). Single-line comments begin with two forward slash characters (//), and multiline comments begin with a slash and an asterisk (/*) and end with the same combination reversed (*/). In these aspects, C# is identical to C++ and Java but different from Visual Basic. It is the semicolons and curly braces that give C# code such a different visual appearance from Visual Basic code. If your background is predominantly Visual Basic, take extra care to remember the semicolon at the end of every statement. Omitting this is usually the biggest single cause of compilation errors among developers who are new to C-style languages. Another thing to remember is that C# is case sensitive. That means the variables named myVar and MyVar are two different variables.

      The first few lines in the previous code example are related to namespaces (mentioned later in this chapter), which is a way to group associated classes. The namespace keyword declares the namespace with which your class should be associated. All code within the braces that follow it is regarded as being within that namespace. The using declaration specifies a namespace that the compiler should look at to find any classes that are referenced in your code but aren’t defined in the current namespace. This serves the same purpose as the import statement in Java and the using namespace statement in C++.

      The reason for the presence of the using static declaration in the Program.cs file is that you are going to use a library class: System.Console. The using static System.Console declaration enables you to refer to the static members of this class and omit the namespace and class names. Just declaring using System; instead, you need to add the class name for calling the WriteLine method:

      Omitting the complete using declaration, you need to add the namespace name invoking the WriteLine method:

      The standard System namespace is where the most commonly used .NET types reside. It is important to realize that everything you do in C# depends on .NET base classes. In this case, you are using the Console class within the System namespace to write to the console window. C# has no built-in keywords of its own for input or output; it is completely reliant on the .NET classes.

      NOTE Because almost every sample in this and the next chapters makes use of static members of the Console class, we will assume that a using static System.Console; statement is present in the file for all code snippets.

      Within the source code, a class called Program is declared. However, because it has been placed in a namespace called Wrox.HelloWorldApp, the fully qualified name of this class is Wrox.HelloWorldApp.Program:

      All C# code must be contained within a class. The class declaration consists of the class keyword, followed by the class name and a pair of curly braces. All code associated with the class should be placed between these braces.

      The class Program contains a method called Main. Every C# executable (such as console applications, Windows applications, Windows services, and web applications) must have an entry point – the Main method (note the capital M).

      The method is called when the program is started. This method must return either nothing (void) or an integer (int). Note the format of method definitions in C#:

      Here, the first square brackets represent certain optional keywords. Modifiers are used to specify certain features of the method you are defining, such as from where the method can be called. In this case the Main method doesn’t have a public access modifier applied. You can do this in case you need a unit test for the Main method. The runtime doesn’t need the public access modifier applied, and it still can invoke the method. The static modifier is required as the runtime invokes the method without creating an instance of the class. The return type is set to void, and in the example parameters are not included.

      Finally, we come to the code statement themselves:

      In this case, you simply call the WriteLine method of the System.Console class to write a line of text to the console window. WriteLine is a static method, so you don’t need to instantiate a Console object before calling it.

      Now that you have had a taste of basic C# syntax, you are ready for more detail. Because it is virtually impossible to write any nontrivial program without variables, we start by looking at variables in C#.

Working with Variables

      You declare variables in C# using the following syntax:

      For example:

      This statement declares an int named i. The compiler won’t actually let you use this variable in an expression until you have initialized it with a value.

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

      You can also declare the variable and initialize its value at the same time:

      If you declare and initialize more than one variable in a single statement, all the variables will be of the

Скачать книгу