C# Creating and Managing Classes

We have previously outlined classes, and a console is a class. With a class you can create your own methods and events, WriteLine being one of them. A class is one way of organizing your code, and the .NetFramework has an extensive amount of classes. For example, if you were collecting details of cars people owned you would collect this information:

  • Manufacturer
  • Model
  • Colour
  • Features
  • Registration Number
  • Cost

This is the minimum amount of data you would probably collect,as you could collect many more points such as the mileage, previous owners, as well s any other information you need when requesting an insurance quote.

  1. Create a new C# Console Application and name it Classes. 
  2. In solution explorer right click on Classes, add a new class file and name it Car (see image below):

Visual Studio 2010 - Solution Explorer - Classes

Now open up the car.cs file and you should see this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Classes
    class car

Making a class starts with the keyword class, as seen above, followed by the class name. Next, inside the body go your methods. We are going to make three (3) methods: we will collect the vehicle manufacturer, car colour, and car model. We create the methods in the same way as we did in the methods tutorial, however there is a slight difference when accessing them. Copy this code and place it in the car class:

public string model(string vModel)
 return vModel;

public string manufacturer(string vManufacturer)
 return vManufacturer;

public string colour(string vColour)
 return vColour;

So far we have not added any code, we are just returning the parameter. Now in the Program.cs file in the method static void main, put this code:

car myCar = new car();

Console.WriteLine("Please enter your car manufacturer: ");
string m =  myCar.manufacturer(Console.ReadLine());

Console.WriteLine("Please enter car model :");
string mo =  myCar.model(Console.ReadLine());

Console.WriteLine("Please enter car colour");
string c =   myCar.colour(Console.ReadLine());

Console.WriteLine("Your car details: \n " + m + " \n " + mo + "\n " + c );

Code Explained

First this appears at the top:

car myCar = new car();

Now when accessing methods in a class you first need to create a variable and then initialize it. Here we have the variable myCar and then initialized it as new car (object). However, you cannot do this:

car myCar = 24;

C# does not allow you to do this, as car is a class. After the first line you have similar statements that have been used in previous tutorials when getting users' input. When you type myCar. you will get a list of the methods this class has:

C# List of methods available to class car

Here you can see the methods that we created are available. When you write myCar.manufacturer( you will get another tool tip which tells you the arguments this takes, which is string vManufacuter as specified in the class.

Public vs Private

In this example we used a public string, which allows this method to be accessed inside of the current class as well as on the outside of the class. If we change this to a private string, then the method is not accessible from the outside, only from the inside of the class.

[ Continue ]