1 00:00:06,810 --> 00:00:07,260 Hello. 2 00:00:07,770 --> 00:00:10,790 In this lecture, we'll talk about question Tan. 3 00:00:11,130 --> 00:00:14,880 What is the difference between this boss and finalized methods? 4 00:00:15,450 --> 00:00:19,440 The dispose method is used to free unmanaged resources. 5 00:00:19,770 --> 00:00:25,970 The finalized method is the same thing as the distractor, so it's the method that is caught on an object 6 00:00:25,980 --> 00:00:29,010 when it is being cleaned up by the garbage collector. 7 00:00:29,550 --> 00:00:36,360 First, let's focus on the displacement that this method comes from a disposable interface, and it 8 00:00:36,360 --> 00:00:42,090 is used to release and unmanaged resources used by an object when its work is finished. 9 00:00:42,480 --> 00:00:48,020 First of all, let's understand what managed and unmanaged resources are managed. 10 00:00:48,020 --> 00:00:54,090 The resources, as their name suggests, are managed by the common language runtime, and the objects 11 00:00:54,090 --> 00:00:56,880 you create with C-sharp are managed resources. 12 00:00:57,150 --> 00:01:02,790 The garbage collector is aware of their existence, and once they are no longer needed, it will free 13 00:01:02,790 --> 00:01:04,470 up the memory they occupy. 14 00:01:04,740 --> 00:01:10,080 This means we don't need to worry about managing resources cleanup as it is done automatically. 15 00:01:10,080 --> 00:01:14,760 For us, unlimited resources are beyond the realm of the C.R.. 16 00:01:15,150 --> 00:01:20,370 The garbage collector doesn't know about them, so it will not perform and the cleanup on them. 17 00:01:20,670 --> 00:01:22,770 Examples of unlimited resources. 18 00:01:23,010 --> 00:01:24,580 Our database connections. 19 00:01:24,630 --> 00:01:26,730 File hangers come objects. 20 00:01:26,910 --> 00:01:27,390 Opened. 21 00:01:27,390 --> 00:01:34,410 Network connections, etc. We as developers are responsible to perform the cleanup after we are done 22 00:01:34,410 --> 00:01:35,550 with those objects. 23 00:01:35,880 --> 00:01:38,160 If we don't, but things may happen. 24 00:01:38,400 --> 00:01:44,500 For example, if we open a file to read it and we don't close it, the next attempt to open the same 25 00:01:44,520 --> 00:01:49,560 file will fail with an error, saying that the file is currently in use. 26 00:01:50,070 --> 00:01:56,760 If we have a class that uses some unmanaged resources, it should implement the AI disposable interface 27 00:01:56,760 --> 00:02:00,030 and provide an implementation of the dispose method. 28 00:02:00,540 --> 00:02:05,040 The disposal method should contain the code that cleans the unmanaged resource. 29 00:02:05,340 --> 00:02:07,350 For example, closes a file. 30 00:02:07,680 --> 00:02:10,080 Let's see a simple cross that does so. 31 00:02:11,100 --> 00:02:15,870 This glass is simply providing a way to eat foul line by line. 32 00:02:16,350 --> 00:02:22,590 Please note that this implementation is simplified for example sake, so it doesn't contain an error. 33 00:02:22,590 --> 00:02:30,450 Handling this class implements a disposable interface, and because of that, it is forced to provide 34 00:02:30,450 --> 00:02:32,820 the implementation of the dispose method. 35 00:02:33,120 --> 00:02:36,570 In this method, we clean up and unmanaged resources. 36 00:02:36,960 --> 00:02:40,680 In this case, we simply called this prismatic on the stream reader. 37 00:02:41,370 --> 00:02:46,950 This is a very common practice when implementing the dispose method because it rarely happens that we 38 00:02:46,950 --> 00:02:53,220 need to access on the managed resources that do not have and this issue class needs to use them provided. 39 00:02:53,940 --> 00:02:56,970 All right, let's use the Federal Reserve class. 40 00:02:58,650 --> 00:03:05,940 At the first glance, it may seem OK, line one and line two are set to those coming from the input 41 00:03:05,940 --> 00:03:06,900 text file. 42 00:03:07,320 --> 00:03:13,770 The problem here is that we don't actually call Did this post mattered and this 300ER is never closed. 43 00:03:14,370 --> 00:03:15,300 Let's fix that. 44 00:03:15,720 --> 00:03:18,030 We can call the displacement of manually. 45 00:03:22,020 --> 00:03:28,500 But this is a bit awkward and easy to forget, not to mention that exception will be thrown in this 46 00:03:28,500 --> 00:03:28,970 court. 47 00:03:29,140 --> 00:03:31,560 The post method will never be called. 48 00:03:31,800 --> 00:03:34,260 It's better to use the U.S. statement. 49 00:03:40,460 --> 00:03:45,440 Starting with the shop old, we can use the following syntax we felt braces. 50 00:03:49,860 --> 00:03:54,540 Remember, the using statement is just syntactic sugar for this. 51 00:03:55,560 --> 00:04:01,770 The final block is used to ensure that the dispose method will be called, no matter if the exception 52 00:04:01,770 --> 00:04:03,240 will be thrown or not. 53 00:04:03,690 --> 00:04:09,450 You can learn more about the using statement in the what is the use of the using keyword lecture? 54 00:04:10,050 --> 00:04:16,230 One more thing before we move on to the finalized method, remember that the garbage collector does 55 00:04:16,230 --> 00:04:17,970 not call did this post method? 56 00:04:18,300 --> 00:04:19,800 It's not even aware of it. 57 00:04:20,160 --> 00:04:26,370 We must call it ourselves, and the best way to do it is by using the using statement, which ensures 58 00:04:26,370 --> 00:04:28,470 that the response method will be called. 59 00:04:29,160 --> 00:04:31,920 Now, let's move on to the finalized method. 60 00:04:32,160 --> 00:04:37,230 This method is called on an object when it is being cleaned up by the garbage collector. 61 00:04:37,500 --> 00:04:40,800 This means it can only be added to reference types. 62 00:04:41,040 --> 00:04:48,280 So, for example, a struct can't have a finalized or defined piece notice that in C-sharp the destructor, 63 00:04:48,390 --> 00:04:52,590 the final riser and the finalized method are the same things. 64 00:04:53,160 --> 00:04:56,430 We can't even define the finalized method in the class. 65 00:04:56,880 --> 00:04:59,400 We must do so by defining the structure. 66 00:04:59,760 --> 00:05:01,260 Let's see this in practice. 67 00:05:02,280 --> 00:05:09,510 As you can see, declaring the finalized method doesn't work as the process does not overwrite object 68 00:05:09,510 --> 00:05:10,260 finalize. 69 00:05:10,530 --> 00:05:12,600 Instead, provide the structure. 70 00:05:13,060 --> 00:05:13,830 Let's do it. 71 00:05:17,400 --> 00:05:23,980 And purity, as you can see, the destructor doesn't have an access modifiers or return type. 72 00:05:24,160 --> 00:05:26,770 And it is preceded by the tilde symbol. 73 00:05:27,490 --> 00:05:28,180 All right. 74 00:05:28,450 --> 00:05:34,540 So we defined a destructor in the person cross when an object of this class will be cleaned up by the 75 00:05:34,540 --> 00:05:35,560 garbage collector. 76 00:05:35,740 --> 00:05:37,750 This method will be executed. 77 00:05:38,200 --> 00:05:40,480 Let me run some code that shows that. 78 00:05:41,810 --> 00:05:49,130 John Object leaves in the scope of some method, and after this method finishes, it is no longer needed 79 00:05:49,550 --> 00:05:51,720 by running GC correct command. 80 00:05:51,770 --> 00:05:54,770 I can ask the garbage collector to do its work. 81 00:05:55,130 --> 00:05:56,870 Let's see the result of this program. 82 00:05:58,860 --> 00:06:01,860 As you can see, the destructor has been executed. 83 00:06:03,120 --> 00:06:05,730 We now know how to define these structures. 84 00:06:05,940 --> 00:06:08,460 So it's time to learn when to use them. 85 00:06:08,820 --> 00:06:11,790 Well, the answer is almost never. 86 00:06:12,030 --> 00:06:16,140 And then quoting Eric Lippert, one of the designers of C-sharp. 87 00:06:16,590 --> 00:06:22,650 As we noted before, if a class is using some unmanaged resources that must be cleaned up, it should 88 00:06:22,650 --> 00:06:25,110 implement a disposable interface. 89 00:06:25,560 --> 00:06:32,190 Some people think that having a destructor that caused the dispose method can be an assurance that those 90 00:06:32,190 --> 00:06:33,960 resources will be cleaned up. 91 00:06:33,990 --> 00:06:38,580 If someone forgets to call the disbursement that manually or if they're using statement. 92 00:06:46,470 --> 00:06:52,680 But this is solving an issue that shouldn't happen at all if developers know what they're doing. 93 00:06:52,920 --> 00:06:55,860 So in the process, we may cause much more problems. 94 00:06:56,340 --> 00:07:03,120 Let me quote Mr. Lippert again if you make a destructor, be extremely careful and understand how the 95 00:07:03,120 --> 00:07:04,470 garbage collector works. 96 00:07:04,860 --> 00:07:06,870 The structures are really weird. 97 00:07:07,170 --> 00:07:08,880 They don't run on your thread. 98 00:07:09,240 --> 00:07:10,860 They run their own thread. 99 00:07:11,070 --> 00:07:16,860 Don't cause deadlocks on 100 exception thrown from a distractor is bad news. 100 00:07:17,040 --> 00:07:18,300 It's only its second thread. 101 00:07:18,420 --> 00:07:19,620 Who's going to catch it? 102 00:07:20,370 --> 00:07:22,170 Destruct or may never run. 103 00:07:22,590 --> 00:07:27,000 You can't rely on the object ever being scheduled for finalization. 104 00:07:27,360 --> 00:07:30,630 It probably will be, but that's not a guarantee. 105 00:07:31,170 --> 00:07:34,040 Almost nothing that is normally true is true. 106 00:07:34,320 --> 00:07:36,960 This to be really, really careful. 107 00:07:37,290 --> 00:07:41,930 Writing a correct destructor is very difficult, actually. 108 00:07:41,940 --> 00:07:47,790 Eric Report mentioned that the only scenario when he needed to actually write these tractors was when 109 00:07:47,790 --> 00:07:51,180 testing the part of the compiler that handles these tractors. 110 00:07:51,360 --> 00:07:53,970 She never intended to do so in production code. 111 00:07:54,510 --> 00:08:00,270 If you want to learn more about the tricky beasts that these structures are, I think some resources 112 00:08:00,330 --> 00:08:02,460 in the file attached to this lecture. 113 00:08:02,850 --> 00:08:06,540 So the bottom line here is do not raise this tractors. 114 00:08:06,750 --> 00:08:12,770 If your objects must clean up some resources after they finish their work, make them implement the 115 00:08:12,780 --> 00:08:14,460 widest possible interface. 116 00:08:15,240 --> 00:08:20,550 Let's summarize the this POS method is used to free unmanaged resources. 117 00:08:20,940 --> 00:08:27,060 The finalized method is the same thing as the destructor, so it's the method that is scored on an object 118 00:08:27,150 --> 00:08:29,850 when it is being cleaned up by the garbage collector. 119 00:08:30,450 --> 00:08:36,810 The topic of the dispose and finalize method is pretty common during the interviews, so you may hear 120 00:08:36,810 --> 00:08:43,560 questions like what is the difference between a destruct or a final laser under the finalized method? 121 00:08:44,070 --> 00:08:46,860 There is no difference as they are the same thing. 122 00:08:47,400 --> 00:08:53,490 During the compilation, the destructor gets turned to the finalized method, which is commonly called 123 00:08:53,520 --> 00:08:54,540 a Finisar. 124 00:08:55,170 --> 00:08:58,080 Thus, the garbage collector call the displacement it. 125 00:08:58,620 --> 00:09:02,080 No, the garbage collector is not aware of this method. 126 00:09:02,340 --> 00:09:09,190 We must call it ourselves, usually by using the using statement When should we write our own these 127 00:09:09,190 --> 00:09:09,990 structures? 128 00:09:10,590 --> 00:09:13,140 The safest answer is almost never. 129 00:09:13,650 --> 00:09:18,450 These structures are very tricky, and we don't even have a guarantee that they will run. 130 00:09:18,900 --> 00:09:21,360 We should use the deports method instead. 131 00:09:22,140 --> 00:09:25,020 What are managed and unmanaged resources? 132 00:09:25,530 --> 00:09:32,010 The managed resources are managed by the common language runtime or objects created with C-sharp and 133 00:09:32,010 --> 00:09:33,150 managed resources. 134 00:09:33,690 --> 00:09:39,720 The garbage collector is aware of their existence and it will clean up the memory they occupy once they 135 00:09:39,720 --> 00:09:40,650 are not needed. 136 00:09:40,950 --> 00:09:45,870 We don't need to worry about human resources when it comes to cleanup and manage. 137 00:09:45,870 --> 00:09:52,500 Resources are beyond the scope of the CRL are the garbage collector doesn't know about them and it will 138 00:09:52,500 --> 00:09:54,060 not release them on. 139 00:09:54,060 --> 00:09:58,500 The manage resources are things like database connections or file handlers. 140 00:09:58,890 --> 00:10:05,760 We, as developers are responsible to perform the cleanup after we are done working with unmanaged resources. 141 00:10:06,420 --> 00:10:08,550 All right, that's it for this lecture. 142 00:10:09,000 --> 00:10:11,460 Thanks for watching and see you in the next one.