Classes are a fundamental aspect of object orientated programming, and the topic is a very broad subject which will be covered in these tutorials.
Classes encompass the whole concept of Object Orientated Programming, which can be a confusing subject and difficult to get your head around. If you are not familiar with the concept of Object Orientation, we recommend that you read our
We are going to stop using Hello World from now on, and instead focus or more real life examples in order to teach classes and object orientated programming.
A C# application is a collection of classes, structures and types.
Firstly, classes and structs are essentially the same, apart from two differences. Classes are a reference type, structs are value types and classes can use inheritance whereas structs cannot. We will cover reference and value types in the next tutorial, and inheritance in a later tutorial.
Microsoft defines a class as
a set of data combined with methods (or functions) that can manipulate that data.
Defining a Class
A class is introduced using the class keyword, followed by the name of the class you are creating. Because a class is a block of code, it requires the use of curly brackets (braces).
Let's use the example in the Object Orientated Primer and create a Car class.
{
}
In the primer we identified several fields and methods associated with a car class. We are going to create a baseCarClass, so create a new console project, create a new class and have a go at adding these field and methods to it. You will need to decide what data type would be best suited to that particular field. For any methods, please fill out a dummy method with no parameters and no return type.
Here is one of the values that we identified in the primer:
- is engine running - Field
Here are a few more that we are going to use in these tutorials. You will need to decide if the item should be a field or a method, and the data type.
- start engine
- accelerate
- brake
- speed
- stop engine
- distance travelled
The compiler is not affected by the order in which you declare fields or methods; however it greatly improves readability if you declare fields and properties at the start of a class, followed by its methods.
{
public bool isEngineRunning;
public decimal speed;
public decimal distanceTraveled;
public void startEngine()
{
}
void accellerate()
{
}
void brake()
{
}
void stopEngine()
{
}
}
You will notice in this code that I have used a new keyword here, "public", before I declare a variable. This will be covered in more depth when we talk about Access Modifiers and Scope, but for now public means that any method or class can access this value. You may also notice that I haven't used the static keyword, as I have done previously. This is because static means that the method or property can be accessed without creating an object.
Because we have not used the static keyword, we must create an instance of the class. This is done using the "new" keyword.
In your Main method, create an instance of the baseCarClass:
{
baseCarClass myCar = new baseCarClass();
}
A class is a user-defined type so we use the same syntax as declaring an int (for example), but a class also needs initialising with the new keyword. This wills instantiate an instance of the class on the heap.
On the next line, type in myCar and a full stop. Notice that the Intellisense will popup and show us the methods and fields of our class, as well as a few other properties (Equals, GetHashCode, GetType and ToString).
No comments:
Post a Comment