Exercise 3.1 Define a class and namespace to manage a road trip - Part 1


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

  • Create a brand new C# project.
  • Remove the existing code from the project.
  • Create a new RoadTrip class which will be used to model our road trip information.

Steps


Below are the step-by-step instructions to implements 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 theRoadTripclass

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 theFile > 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
  {
  }
}

results matching ""

    No results matching ""