1 00:00:06,240 --> 00:00:06,590 Hello. 2 00:00:07,950 --> 00:00:11,710 In this lecture, we're going to talk about Topic 34. 3 00:00:12,060 --> 00:00:14,490 What is operator overloading? 4 00:00:15,120 --> 00:00:24,570 C-sharp provides many operators, for example, plus minus plus plus, etc. The important thing to understand 5 00:00:24,570 --> 00:00:31,320 about operators is that their behavior differs depending on what types they are used with. 6 00:00:31,860 --> 00:00:39,180 For example, adding two numbers with the Plus operator will simply calculate the sum of numbers while 7 00:00:39,180 --> 00:00:46,680 adding two strings with the same operator will concatenate those two strings when defining our own types. 8 00:00:46,890 --> 00:00:51,390 We would often like to provide a custom implementation for some operators. 9 00:00:51,870 --> 00:00:54,360 Let's consider a simple point type. 10 00:00:55,050 --> 00:00:58,830 We would like to define the operation of adding two points. 11 00:00:59,340 --> 00:01:04,620 It should work by adding the R, X and Y coordinates, for example. 12 00:01:05,100 --> 00:01:10,740 Adding those two points should give a new point, which coordinates seven nine. 13 00:01:11,400 --> 00:01:16,380 We can achieve it by defining the other method in the point records reconstruct. 14 00:01:23,340 --> 00:01:30,720 This is correct, but it's a bit awkward to use to add to points will need to write something like this. 15 00:01:35,000 --> 00:01:39,350 It would be more convenient to perform the audition with the Plus operator. 16 00:01:39,680 --> 00:01:42,230 It is, after all, the audition operator. 17 00:01:42,530 --> 00:01:44,940 Unfortunately, this doesn't work. 18 00:01:47,030 --> 00:01:53,960 The compiler doesn't yet know how to add two points to enable the addition of two objects of this type. 19 00:01:54,050 --> 00:01:56,630 We must overload the addition operator. 20 00:02:06,530 --> 00:02:13,670 As you can see, to overload the operator, we must define a static method using the operator keyword. 21 00:02:14,150 --> 00:02:19,550 We must define the parameters and the written type just like in regular methods. 22 00:02:20,000 --> 00:02:25,370 In the case of an addition, there are two operations, so we have two parameters. 23 00:02:25,850 --> 00:02:30,500 Remember, the operand is the thing to which the operator is applied. 24 00:02:30,830 --> 00:02:36,740 For example, when adding three plus five, we'll have two operands three and five. 25 00:02:37,760 --> 00:02:42,320 Please note that some operators take more or less operands. 26 00:02:42,710 --> 00:02:48,740 For example, the plus plus operator that increases the number by one takes one operand. 27 00:02:51,730 --> 00:02:58,330 On the other hand, the ternary conditioner operator takes free operations, the condition value, if 28 00:02:58,330 --> 00:03:00,550 true, and value a false. 29 00:03:08,050 --> 00:03:08,750 All right. 30 00:03:09,190 --> 00:03:12,370 We overloaded the addition operator, four point type. 31 00:03:12,520 --> 00:03:14,980 And now this started to work. 32 00:03:15,550 --> 00:03:20,320 We can overload most of the C-sharp operators, but not all of them. 33 00:03:20,680 --> 00:03:23,980 For example, we can't overload alarm the operator. 34 00:03:24,190 --> 00:03:25,990 No access operator. 35 00:03:26,260 --> 00:03:34,120 New operator In the document attached to this lecture, I leaked the full list of overall operators. 36 00:03:34,780 --> 00:03:41,530 I don't want to show you the overload for all operators because the code would mostly be the same. 37 00:03:41,860 --> 00:03:49,060 But let me show you two interesting and commonly used operators the explicit and implicit conversion 38 00:03:49,060 --> 00:03:49,960 operators. 39 00:03:50,230 --> 00:03:55,210 First, let me show you some examples of their use it for built in types. 40 00:04:00,240 --> 00:04:05,340 This goat looks innocent, but there is more going on here than it seems. 41 00:04:05,640 --> 00:04:08,970 After all, we are an integer to a double. 42 00:04:09,330 --> 00:04:12,360 There are two different types, so how does it work? 43 00:04:12,690 --> 00:04:16,050 Well, it works because implicit conversion happens. 44 00:04:16,470 --> 00:04:20,220 The a integer is implicitly converted to a double. 45 00:04:20,760 --> 00:04:23,790 Now, let's see the opposite assignment. 46 00:04:29,390 --> 00:04:31,700 As you can see, this doesn't work. 47 00:04:31,940 --> 00:04:38,540 You might be asking, why did it work when assigning an end to a double, but it doesn't work for the 48 00:04:38,540 --> 00:04:39,740 opposite operation. 49 00:04:40,160 --> 00:04:45,950 The reason for that is simple the conversion of an end to a double is lossless. 50 00:04:46,400 --> 00:04:51,620 The double can represent the value of five and all stored in an indoor variable. 51 00:04:51,920 --> 00:04:58,700 On the other hand, the integer country presented the number five and half one, converting five and 52 00:04:58,700 --> 00:04:59,810 a half to end. 53 00:04:59,960 --> 00:05:02,900 We will lose some accuracy of the data. 54 00:05:03,200 --> 00:05:06,020 The result will be trimmed to a full five. 55 00:05:06,470 --> 00:05:13,520 That's why we must perform such conversion explicitly so there is no chance we'll do it by accident 56 00:05:13,760 --> 00:05:15,660 when using explicit cost. 57 00:05:15,680 --> 00:05:22,400 We say I know what they are doing, and I'm aware that the value might actually change during the conversion. 58 00:05:22,700 --> 00:05:25,580 I'm ready to take this risk and handle it. 59 00:05:27,630 --> 00:05:35,370 Here I performed the explicit conversion from five and a half double to end the result will be five. 60 00:05:36,060 --> 00:05:40,740 It's quite a common use case that we want to overload the conversion operators. 61 00:05:41,250 --> 00:05:43,650 Let's go back to the point type example. 62 00:05:44,160 --> 00:05:51,540 Let's say that our application is getting the points data from some external source and that the points 63 00:05:51,540 --> 00:05:54,060 are delivered to us as valid oppose. 64 00:05:54,480 --> 00:06:01,080 We would like to be able to simply assign a couple of two floats to a variable of point type, thus 65 00:06:01,080 --> 00:06:03,270 performing implicit conversion. 66 00:06:08,140 --> 00:06:14,950 Well, it doesn't work, the compiler doesn't know how to cast a tattoo of two numbers to the point 67 00:06:14,950 --> 00:06:15,460 type. 68 00:06:15,640 --> 00:06:19,480 We must implement our own implicit conversion operator. 69 00:06:36,710 --> 00:06:39,260 And now this code works. 70 00:06:39,860 --> 00:06:47,000 We can also overload the explicit conversion operator, if only the explicit conversion operator is 71 00:06:47,000 --> 00:06:47,900 implemented. 72 00:06:48,110 --> 00:06:51,530 We'll have to come to the table to point explicitly. 73 00:06:52,190 --> 00:06:58,280 Let's comment out the implicit conversion operator and at the explicit conversion operator. 74 00:07:03,010 --> 00:07:08,290 Now, to move this called work, I must perform on explicit cost. 75 00:07:12,090 --> 00:07:20,070 And now everything works as expected as you can see for conversion operators, overloading the explicit 76 00:07:20,070 --> 00:07:22,410 or implicit keywords are needed. 77 00:07:23,100 --> 00:07:29,460 Before we wrap up, let's think about what we should use the implicit and one the explicit conversion 78 00:07:29,460 --> 00:07:31,020 operator of a loading. 79 00:07:31,410 --> 00:07:38,730 We can safely use implicit casting when the cost is lossless, so it won't change the underlying data. 80 00:07:39,090 --> 00:07:43,680 For example, it won't change its precision in case of the pot struct. 81 00:07:43,860 --> 00:07:48,270 The implicit conversion operator was found in all other cases. 82 00:07:48,420 --> 00:07:56,070 If the conversion is lossless, we should use explicit casting, so no data losing operations are executed 83 00:07:56,070 --> 00:07:57,120 behind the scenes. 84 00:07:57,210 --> 00:07:59,490 We found the programmers intention. 85 00:08:00,090 --> 00:08:07,630 Let's summarize We can provide custom behavior for operators in our own types by using operators of 86 00:08:07,630 --> 00:08:08,370 the loading. 87 00:08:08,670 --> 00:08:12,660 We can overload most but not all, C-sharp operators. 88 00:08:13,080 --> 00:08:18,180 We can also overload the implicit and explicit conversion operators. 89 00:08:18,780 --> 00:08:25,230 The questions related to this topic that you can hear during the interview could be What is the purpose 90 00:08:25,230 --> 00:08:26,790 of the operator keyword? 91 00:08:27,330 --> 00:08:32,160 Well, this keyword is used when overloading an operator for a time. 92 00:08:33,060 --> 00:08:37,110 What's the difference between explicit and implicit conversion? 93 00:08:37,650 --> 00:08:45,120 Implicit conversion happens when we assign a value of one type to a variable of another type without 94 00:08:45,120 --> 00:08:48,120 specifying the target type in parentheses. 95 00:08:48,420 --> 00:08:55,980 For example, it happens when assigning an end to a double explicit conversion requires specifying the 96 00:08:55,980 --> 00:08:57,390 type in parentheses. 97 00:08:57,630 --> 00:09:03,900 For example, when assigning a double to one end, all right, that's it in this lecture. 98 00:09:04,170 --> 00:09:06,960 Thanks for watching, and I see you in the next one.