Could anybody please explain to me in a very simple way (compare it to something in real life) what objects are, what a class is and what operators are and what all of these do?

First you should understand:

- Functions

- Basic data types (int, float, string)

- Variables

Assuming you understand the above. Basic data types like integers and floats are great for representing single, scalar values, for example someone's height.

float height = 1.88m; // my height in meters

It's also easy to write functions that operate on single values using these types. Let's write a function that takes two floats and returns their sum:

float Sum(float a, float b) {
return a + b;
}

We can call this function like this:

float result = Sum(3f, 5f);
// now result is 8f

However, it becomes a problem when you want to represent composite values. For example, how would you represent a 2D coordinate in C#? Perhaps with two variables:

float X = 12f;
float Y = 15f;

Now, how would you write a function that takes two 2D coordinates and returns their sum? Note that the sum of two 2D points is a 2D point, i.e. mathematically:

(X', Y') = (X1, Y1) + (X2, Y2)

where

X' = X1 + X2

Y' = Y1 + Y2

So, you would need to take in 4 values (X1, Y1, X2 and Y2) and return 2 values (X' and Y'). Something like:

(X, Y) = Sum(x1, y1, x2, y2);

Alas, this is not valid C#! A function can only return one value; there is no way to write a Sum function such that this code will compile.

We'd need to somehow return our two floats as a single value...

Which is exactly where classes come in! In C#, we can define a new data type by compositing existing data types, like so:

class Point2D {
public float X;
public float Y;
}

This allows us to write our Add function easily:

Point2D Sum(Point2D a, Point2D b) {
var result = new Point2D();
result.X = a.X + b.X;
result.Y = a.Y + b.Y;
return result;
}

Here, Point2D is a class. a, b, and result are *objects*, i.e. instances of a class. We could have as many Point2D objects as we want (i.e. instances), but there's only one Point2D *class *(only one definition of Point2D). There's some new syntax here: the *new operator* and the *dot operator*. These allow us to create an object and access its members, respectively.

Try not to stumble too much on the syntax. Simply understand that we defined a new type, "Point2D", as a composite of two floats, which we named X and Y. We can now create all the Point2Ds we want, just like we could create all the floats or strings we want. Point2D is a new data type, our *own *data type, and it has two members. We can now define functions that create and operate on this type. This allows us to *use related data as a single value*.

We could use our new Sum function like so:

var p1 = new Point2D();
p1.X = 10;
p1.Y = 10;
var p2 = new Point2D();
p2.X = 13;
p2.Y = 15;
var p3 = Sum(p1, p2);
Console.WriteLine("({0},{1}) + ({2},{3}) = ({4},{5})", p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
// prints "(10,10) + (13,15) = (23,25)"

Now, writing:

var p3 = Sum(p1, p2);

(where p1, p2 and p3 are Point2Ds) is ok, but it'd be much nicer if just like we do with floats, we could more naturally write:

var p3 = p1 + p2;

To do this, we need to define what the operator "+" means, when applied to two Point2Ds. Intuitively enough, we need to modify our definition of Point2D to specify this meaning:

class Point2D {
public float X;
public float Y;
public static Point2D operator +(Point2D p1, Point2D p2)
{
var result = new Point2D();
result.X = p1.X + p2.X;
result.Y = p1.Y + p2.Y;
return result;
}
}

Again, try not to focus too much on the syntax. We're defining "operator +" as a function that takes two Point2Ds and returns their sum, i.e. another Point2D. And now, the above code just works!

Look ma' I just learned operator overloading!

Finally, it'd also be nice if it didn't require three lines of code to create and initialize a Point2D:

var p1 = new Point2D();
p1.X = 3;
p1.Y = 5;
// All this typing!!! URRRGH

Wouldn't it be nice if we could just write this in one line, say like so:

var p1 = new Point2D(3, 5);
// AAAAH WAY BETTER

And this would automatically assign 3 to X and 5 to Y. C# allows us to do this with special methods called *constructors.* So let's modify the Point2D class again:

class Point2D
{
public float X;
public float Y;
public Point2D(float x, float y)
{
X = x;
Y = y;
}
public static Point2D operator +(Point2D p1, Point2D p2)
{
return new Point2D(p1.X + p2.X, p1.Y + p2.Y);
}
}

The strange Point2D method with no return type is what's called a constructor. It allows us to write the initialization logic in only one place, and then create our objects much more easily.

Notice that I also changed the "operator +" function to use this constructor directly instead of creating a Point2D and initializing the members separately: it now fits nicely on a single line.

We can go further and simplify all of our code. For instance, we can rewrite our addition example like so:

var p1 = new Point2D(10, 10);
var p2 = new Point2D(13, 15);
var p3 = p1 + p2;
Console.WriteLine("({0},{1}) + ({2},{3}) = ({4},{5})", p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
// prints "(10,10) + (13,15) = (23,25)"

Isn't this great? We can now work with 2D coordinates basically as easily as floats! We made our own data type!

And that's about all I'm willing to fit into a forum post. Hopefully you will have understood that:

- classes allow you to define your own data types by compositing existing data types

- classes can define operations on their members (like the "+" operator)

- A class is the definition of the data type: what it *is *and what it *does*. An object is a concrete *instance *of that data type. There's only ever one definition per type (one class), but there can be as many objects of that type as you want.

Take your time. There's lots to learn about classes: public vs protected vs private members, static vs instance members, inheritance, virtual methods, etc. If you were following a good book with exercices and doing the exercices, you would likely not need to keep asking for basic tutorials here. I suggested the Yellow Book many times to you (which would explain all this much better than me), and I can only repeat my suggestion. You don't need to be stuck and wait for people to help you on forums. You can follow a good book like that and practice on your own.

As an exercise, try to extend the Point2D with more operations:

- substraction

- scalar addition/substraction

- dot product

- cross product

- norm

- etc.

Don't hesitate if you have more specific questions though.

Here's a full, compilable listing if you want to play with the example code:

using System;
class Point2D
{
public float X;
public float Y;
public Point2D(float x, float y)
{
X = x;
Y = y;
}
public static Point2D operator +(Point2D p1, Point2D p2)
{
return new Point2D(p1.X + p2.X, p1.Y + p2.Y);
}
}
class Program
{
static void Main()
{
var p1 = new Point2D(10, 10);
var p2 = new Point2D(13, 15);
var p3 = p1 + p2;
Console.WriteLine("({0},{1}) + ({2},{3}) = ({4},{5})", p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
// prints "(10,10) + (13,15) = (23,25)"
}
}