Hello.
In this lecture, we're going to talk about Topic 34.
What is operator overloading?
C-sharp provides many operators, for example, plus minus plus plus, etc. The important thing to understand
about operators is that their behavior differs depending on what types they are used with.
For example, adding two numbers with the Plus operator will simply calculate the sum of numbers while
adding two strings with the same operator will concatenate those two strings when defining our own types.
We would often like to provide a custom implementation for some operators.
Let's consider a simple point type.
We would like to define the operation of adding two points.
It should work by adding the R, X and Y coordinates, for example.
Adding those two points should give a new point, which coordinates seven nine.
We can achieve it by defining the other method in the point records reconstruct.
This is correct, but it's a bit awkward to use to add to points will need to write something like this.
It would be more convenient to perform the audition with the Plus operator.
It is, after all, the audition operator.
Unfortunately, this doesn't work.
The compiler doesn't yet know how to add two points to enable the addition of two objects of this type.
We must overload the addition operator.
As you can see, to overload the operator, we must define a static method using the operator keyword.
We must define the parameters and the written type just like in regular methods.
In the case of an addition, there are two operations, so we have two parameters.
Remember, the operand is the thing to which the operator is applied.
For example, when adding three plus five, we'll have two operands three and five.
Please note that some operators take more or less operands.
For example, the plus plus operator that increases the number by one takes one operand.
On the other hand, the ternary conditioner operator takes free operations, the condition value, if
true, and value a false.
All right.
We overloaded the addition operator, four point type.
And now this started to work.
We can overload most of the C-sharp operators, but not all of them.
For example, we can't overload alarm the operator.
No access operator.
New operator In the document attached to this lecture, I leaked the full list of overall operators.
I don't want to show you the overload for all operators because the code would mostly be the same.
But let me show you two interesting and commonly used operators the explicit and implicit conversion
operators.
First, let me show you some examples of their use it for built in types.
This goat looks innocent, but there is more going on here than it seems.
After all, we are an integer to a double.
There are two different types, so how does it work?
Well, it works because implicit conversion happens.
The a integer is implicitly converted to a double.
Now, let's see the opposite assignment.
As you can see, this doesn't work.
You might be asking, why did it work when assigning an end to a double, but it doesn't work for the
opposite operation.
The reason for that is simple the conversion of an end to a double is lossless.
The double can represent the value of five and all stored in an indoor variable.
On the other hand, the integer country presented the number five and half one, converting five and
a half to end.
We will lose some accuracy of the data.
The result will be trimmed to a full five.
That's why we must perform such conversion explicitly so there is no chance we'll do it by accident
when using explicit cost.
We say I know what they are doing, and I'm aware that the value might actually change during the conversion.
I'm ready to take this risk and handle it.
Here I performed the explicit conversion from five and a half double to end the result will be five.
It's quite a common use case that we want to overload the conversion operators.
Let's go back to the point type example.
Let's say that our application is getting the points data from some external source and that the points
are delivered to us as valid oppose.
We would like to be able to simply assign a couple of two floats to a variable of point type, thus
performing implicit conversion.
Well, it doesn't work, the compiler doesn't know how to cast a tattoo of two numbers to the point
type.
We must implement our own implicit conversion operator.
And now this code works.
We can also overload the explicit conversion operator, if only the explicit conversion operator is
implemented.
We'll have to come to the table to point explicitly.
Let's comment out the implicit conversion operator and at the explicit conversion operator.
Now, to move this called work, I must perform on explicit cost.
And now everything works as expected as you can see for conversion operators, overloading the explicit
or implicit keywords are needed.
Before we wrap up, let's think about what we should use the implicit and one the explicit conversion
operator of a loading.
We can safely use implicit casting when the cost is lossless, so it won't change the underlying data.
For example, it won't change its precision in case of the pot struct.
The implicit conversion operator was found in all other cases.
If the conversion is lossless, we should use explicit casting, so no data losing operations are executed
behind the scenes.
We found the programmers intention.
Let's summarize We can provide custom behavior for operators in our own types by using operators of
the loading.
We can overload most but not all, C-sharp operators.
We can also overload the implicit and explicit conversion operators.
The questions related to this topic that you can hear during the interview could be What is the purpose
of the operator keyword?
Well, this keyword is used when overloading an operator for a time.
What's the difference between explicit and implicit conversion?
Implicit conversion happens when we assign a value of one type to a variable of another type without
specifying the target type in parentheses.
For example, it happens when assigning an end to a double explicit conversion requires specifying the
type in parentheses.
For example, when assigning a double to one end, all right, that's it in this lecture.
Thanks for watching, and I see you in the next one.