Week1 Labs - Lecture notes 1 PDF

Title Week1 Labs - Lecture notes 1
Course C# programing
Institution University of California Los Angeles
Pages 7
File Size 479 KB
File Type PDF
Total Downloads 21
Total Views 148

Summary

Week1 Labs notes...


Description

Programming in C# for Visual Studio .NET Platform I

UCLA Extension Mickey Pujji

Lab 1 – Introduction to Visual Studio In this hands-on lab, you will create a Console application in C# using the Microsoft Visual Studio .NET Integrated Development Environment (IDE). Creating a new project in the Visual Studio IDE Step 1: Launch Visual Studio from the Start menu. The IDE Start page appears as in Figure 1.

Figure 1. Start page in the Visual Studio .NET IDE Note: The Start page opens by default when you start the IDE. It provides links to the last four projects that you have worked on within the environment, and also provides additional resource links. You can change all of the default settings by clicking Tools and then clicking Options to choose your preferences.

Step 2: Click File  New  Project. The New Project window appears as in Figure 2.

Programming in C# for Visual Studio .NET Platform I

UCLA Extension Mickey Pujji

Figure 2. New Project window Step 3: In the Templates pane, click Windows. Step 4: In the right pane, click Console Application. Step 5: In the Name field, type Lab1, choose a location for your project and in the Solution name field, type Labs. Step 6: Click OK. Deleting the Program.cs Source File The project will contain a default program.cs source file. Let’s go ahead and delete this file. Step 1: Right-click on the Program.cs file and click Delete. Creating the Hello.cs Source File The Hello.cs file is the source file used in this lab. You will create the file within the Lab1 project. Step 1: Right-click the project name in the Solution Explorer (Lab1), click Add and then click New Item. The Add New Item window appears as in Figure 3. Step 2: In the right pane, click Class.

Programming in C# for Visual Studio .NET Platform I

Step 3: Type Hello.cs in the name field, and then click Add.

Figure 3. Add New Item window Step 4: Type the following code into the page.

Figure 4. The Hello.cs source file

UCLA Extension Mickey Pujji

Programming in C# for Visual Studio .NET Platform I

UCLA Extension Mickey Pujji

Step 5: Click File, and then click Save the Hello.cs (or click the Save icon) file. The file is saved to your project folder. Note: In C# (unlike C or C++), there are no global methods. All methods belong to a class. The using System; line tells the compiler that all classes and types from the System namespace are available to this program. The system namespace includes basic types, such as int and string. The public class Hello line declares a class named Hello.cs. This means that the Hello.cs class can be accessed by any other class. The static void Main() line is the program's entry point. The entry point is the point where the program, or executable file, starts. This line is required at the beginning of every executable file within a program. The Console.WriteLine("Hello World!"); line uses the WriteLine() method of the Console class within the System namespace. The parameter Hello World! is passed to Console.WriteLine(), and "Hello World!" is displayed on the screen when the program is executed. Console.WriteLine() can be used instead of System.Console.WriteLine() because the System namespace is declared at the top of the file.

Building the Executable File Step 1: To build the executable file from within the IDE, on the Build menu, click Build (or click the Build icon). Note The IDE builds the executable file Lab1.exe and places it in the ~\hello\bin\Debug folder.

During the build, the Output window appears in the IDE as in Figure 5. If any errors occur during the build, they will be reported in this window.

Programming in C# for Visual Studio .NET Platform I

UCLA Extension Mickey Pujji

Figure 5. Output window within the IDE Running the Program You can now run the program from within the IDE. Step 1: Select Debug > Start Without Debugging. Hello World! is printed to a Microsoft MS-DOS® window (Figure 6). Note Because this program is a very small executable file that prints text to an MS-DOS screen, you will run it with debugging turned off. This allows you to see the results. Keep in mind that the "Press any key to continue" line after "Hello World!" is not part of the program. The first time an executable file is run in the .NET framework, it is compiled to native code. This may cause a delay in displaying the results. The executable file runs faster the next time it is executed. No object (.obj) files are created as a result of invoking the C# compiler; output files are created directly. Consequently, the C# compiler does not need a linker.

Programming in C# for Visual Studio .NET Platform I

Figure 6. Hello World! Program results Step 2: Close the MS-DOS window by pressing any key. Closing Out of Lab 1 To close the Hello World! Program, click File, and then click Exit.

UCLA Extension Mickey Pujji

Programming in C# for Visual Studio .NET Platform I

UCLA Extension Mickey Pujji

Lab 2 – Outputting to Console Window In this hands-on lab, you will write a C# Sharp program to print the word “Hello” and your name in a separate line. Creating a new project in the Visual Studio IDE Follow the steps outlined in the previous lab to create a new project called Lab2 in Labs solution. Step 1: Add the following code to the Program.cs file

Figure 1. Program.cs file Step 2: Run the Lab2 without debugging and verify the output results. This concludes Lab2. You may close your solution!...


Similar Free PDFs