Intro
In C#, a class is a user-defined type which can be used to create objects, representing the base of OOP. A class can’t be used directly, so you need to create an instance:
1
2
|
MyClass MyObject = new MyClass();
|
MyClass – is the name of the class
MyObject – is the name of the object that uses that class.
Creating a class
Let’s start with a pretty straightforward example: we have a list of cars, each car has some properties (like speed and color) and some basic actions/functions (like moving or stopping). But how can we store all this data ? Here the classes become useful.We’ll create a class called ‘car’:
* every element in the class must be public in order to be accessible from the outside
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class car
{
// each car will have it's own color and speed
public string color;
public double speed;
// and each car will have it's own function
public void move()
{
//moving...
}
public void stop()
{
//stopping...
}
}
|
Now let’s say we have 10 cars available, so we need 10 instances of our C# class: we can use an array for this:
1
2
3
4
5
6
|
// here's how we define an array cars
// each element in here is different
// so they won't share data if they use the same class
car[] carsList = new car[10];
|
Using the instances of the class
As I said before, every element has it’s own instance, they will NOT share any data. To assign a property for an object you can do something like this:
1
2
3
4
5
6
7
8
9
10
11
|
// first car has a speed of 0 and it's white
carsList[0].color = "white";
carsList[0].speed = 0;
// second car has a speed of 130 and it's blue
cars[1].color = "blue";
cars[1].color = 130;
cars[1].move(); // let's say this one is moving.
//etc.
|
Constructors
If you’ve understood everything above, we can move to a more complex element: constructors.A constructor is a block called when an instance of the class is created – even if it looks like a function, it has no type (it’s not void or anything else) and also must have the exact name of the class (the class and the constructor must share the same name). We’ll use the ‘car’ class again, but with some modifications:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class car
{
public string color;
public double speed;
public car(string color, double speed) // this is the constructor
{
//this.variable is refering to the public variables above (not the parameters)
this.color = color;
this.speed = speed;
}
}
|
The constructor above is called whenever you create an instance (object) :
1
2
3
|
//creating a green-colored car with speed equal with 0
car MyCar = new car("green", 0);
|
Destructors
Destructors (also called finalizers) are used to release the memory you’ve been using while you had an instance. They’re automatically called when program’s closing – however they can’t be called manually.One thing to note: they don’t take any parameters.
Because of C#’s memory management, you don’t really need to use them unless you’re working with unmanaged code. Most of the times, you won’t need to use them.
This is how you declare a destructor for a class:
1
2
3
4
5
6
7
8
|
public class car
{
~car()
{
// clear memory
}
}
|
Sign up here with your email
ConversionConversion EmoticonEmoticon