Learning C# with Xamarin: Object-Oriented Programming

Duration

10 minutes

Lab goals

Here you will implement a RoadTrip class that will eventually store information about a car journey. The high-level goals for the exercise are listed below:

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

  1. Create a new Console Project in Visual Studio. Name the Project and Solution Travel.
  2. Delete the Console.WriteLine that Visual Studio generated inside Main. It should have no code in the Main method.

Create the RoadTrip class

In this section, you will code a class named RoadTrip that will be used to store information about our road trip.

  1. First, you need to create the RoadTrip class. Each C# class typically goes in its own file. This is not an absolute requirement, but it is considered a best-practice. Click on the File > New > File menu entry to launch the New File dialog (see below).
  2. In the New File dialog, choose the Empty File entry from the General category, enter RoadTrip as the Name, then click the New button (see below). Visual Studio will generate an empty file named RoadTrip.cs for you. The Empty File template is a good choice because it will force you to write every line of code from scratch without any help from Visual Studio. Once you get more experience, you can try out some of the other templates; for example, the Empty Class template would have automatically written the outline of the class for you.
  3. Add a namespace declaration to the RoadTrip.cs file as shown below. A namespace is a way of grouping related classes together. Common practice is to include your company name as part of the namespace name (e.g. Xamarin.Training.Samples); however, that is not necessary for this simple program.
    namespace Travel
    {
    }
    
  4. Add the outline of the RoadTrip class to the Travel namespace as shown below.
    namespace Travel
    {
      class RoadTrip
      {
      }
    }
    

Summary

In this lab, you created a new C# project and added a new RoadTrip class. In the next exercise, we will add data to this class to manage our road trip information.

Go Back