Learning C# with Xamarin: Introduction to C#

Duration

30 minutes

Lab goals

This exercise takes you through the entire process of building and running a C# program using Visual Studio. The C# code will be very simple. Most of your time will be spent creating a project, compiling it into an executable program, and running it to test your work. The overall steps are listed below:

Below is the output from the finished application.

- - - - - - - - - MISSING IMAGE - - - - - - - - - -

Required assets

The provided Exercise 1/Part1.Completed folder contains a completed version of the exercise you can use to check your work. Please make sure you have this folder before you begin.

Tip: If you are doing this exercise live in a session, make sure to make good use of the instructor, they are online to answer any questions you have!

Steps

Below are the step-by-step instructions to implement the exercise.

Create a new Console application

You will use Visual Studio to create an application. Visual Studio is an Integrated Development Environment (IDE) which means it has all the tools you need to write, build, and run your program.

  1. Start Visual Studio. You should see something similar to the image shown below.
  2. Create a new Project. Select File > New > Project menu entry (see below).
  3. In the New Project dialog, click Visual C# > Windows Classic Desktop > Console App , you can also type console in the search bar and have Visual Studio find the appropriate template. If you are using Visual Studio for Mac, click the new project button on the start screen and select .NET Core > Console Application and then click next
  4. A Console project interacts with the user through a text-based window. It does not have a Graphical User Interface (GUI) with buttons and text-input areas but it is a great way to get started as a programmer since it requires very little code to get an application running.
  5. Enter HelloWorld in the Name field. The Name field determines the name of the Project.
  6. Notice how the Solution name field gets filled in automatically to match the project name. A Solution is a container that can hold multiple projects. For example, you could have a single solution with both your Android and iOS projects inside it. For solutions that contain only a single project, it makes sense for the project name and the solution name to match.
  7. In the Location field, enter the folder where you would like to store your work.
  8. When you have everything filled in, click OK.
  9. Visual Studio will create the Solution, the Project, and some starter C# code for you (see below). - - - - - - - - - MISSING IMAGE - - - - - - - - - -

Write a custom message to the Console window

Visual Studio adds some convenient starter code for you when you create a new project. In this part, you will spend a few minutes working with Visual Studio to examine this starter code. You will then modify the code so it prints your custom message.

  1. Visual Studio makes it easy to work with your code. The Solution view along the left edge shows you your entire Solution. The text editor in the central area lets you modify the contents of your files. The image below shows these two key areas. - - - - - - - - - MISSING IMAGE - - - - - - - - - -
  2. Visual Studio puts the starter code in a file named Program.cs. The code in the file is called source code and the file is called a source file. C# source files have a .cs extension. The Solution view lists the Program.cs file (see below). - - - - - - - - - MISSING IMAGE - - - - - - - - - -
  3. The Program.cs file should already be open in the text editor. If it is not, double-click on it in the Solution view to open it.
  4. Your C# program can make use of an extensive Standard Library that provides pre-written pieces of code that you can call on to help you get your work done. For example, there are routines to compute square root, search within text, read from a file on disk, etc. The library is divided into namespaces where a namespace is a group of related code. The most common namespace is System. If your program needs to use something from System, you place the line of code shown below at the top of your source file. Your code will need to use the Console class that is part of the System namespace so it should contain the line of code shown below.
    using System;
    
  5. You should put all of your own code inside one of your own namespaces (see below). Most programmers use their company name as part of their namespace name to keep their code clearly labeled and distinct from third-party code that they might incorporate into their projects. Using a namespace is not technically required but it is considered a best practice.
    namespace HelloWorld
    {
      ...
    }
    
  6. Inside your namespace, Visual Studio created a class named MainClass (see below). For now, we can define a class informally as a group of related methods.
    class MainClass
    {
      ...
    }
    
  7. Inside MainClass, Visual Studio created a method named Main (see below). A method is a sequence of program instructions with a name. Other programming languages use the terms function, procedure, or subroutine for this concept. The Main method is the program entry point; that is, it runs automatically when the program is started. The program automatically shuts down when Main completes execution. The C# language defines very precise rules for how Main must be written; for example, the modifiers public and static are required, the name must be Main with an upper-case M, etc.
    class MainClass
    {
      public static void Main(string[] args)
      {
        ...
      }
    }
    
  8. Inside Main there should be a call to Console.WriteLine method (see below). Console is a class and WriteLine is a method defined inside it. You use WriteLine to print something to the Console window (remember that our project type is Console Project so the user will see a Console window automatically when the program runs).
    Console.WriteLine("Hello World!");
    
  9. Change the "Hello World!" text so the program will print a message of your choice.

Build and run the program

In order to test your program, you must first build an executable file from your source code. The precise term for this process is build. The compiler performs the key part of the build process since it translates the source code you wrote into the machine code that the CPU can understand. For our simple program, compilation is the main step of the build process. Other projects such as Android or iOS apps have complicated build processes that require steps such as code generation or the packaging multiple files into a single archive.

  1. To build your project, click on the Build > Build Solution menu entry in Visual Studio (see below). - - - - - - - - - MISSING IMAGE - - - - - - - - - -
  2. If your program is correct, you should see the message Build succeded message in the output window at the bottom of the screen.
  3. If your program has an error, Visual Studio will show you the line that has the problem along with a short description of the issue (see below). Examine the indicated line, fix the problem, and try to build again. - - - - - - - - - MISSING IMAGE - - - - - - - - - -
  4. To run your program, click on the Debug > Start Without Debugging menu entry (see below). - - - - - - - - - MISSING IMAGE - - - - - - - - - -
  5. A Console window should appear on your screen and display the message you passed to Console.WriteLine as shown below. - - - - - - - - - MISSING IMAGE - - - - - - - - - -

Summary

In this lab, you worked through the complete software development cycle: create a project, write source code, build, and execute. You also got a quick overview of how to use Visual Studio. These core skills will be useful as you continue your study and start building applications that are more complex.

Go Back