1 00:00:07,260 --> 00:00:07,720 Hello. 2 00:00:08,280 --> 00:00:16,530 In this video, we're going to talk about Question 49, what are preprocessor directives preprocessor 3 00:00:16,530 --> 00:00:17,470 directives? 4 00:00:17,520 --> 00:00:22,650 Help us control the compensation process from the level of the code itself. 5 00:00:23,130 --> 00:00:27,030 You can choose if some part of the code will be compiled or not. 6 00:00:27,330 --> 00:00:33,900 We can disable or enable some compensation warnings, or we can even check for the doesnAt version and 7 00:00:33,900 --> 00:00:36,870 execute different code, depending on it. 8 00:00:37,290 --> 00:00:43,390 But first, what is a pre process or a preprocessor also known as the pre compiler? 9 00:00:43,470 --> 00:00:51,240 Is the program that runs before the actual compiler that can apply some operations on code before it's 10 00:00:51,240 --> 00:00:52,050 compiled. 11 00:00:52,380 --> 00:00:58,620 Although the C-sharp compiler doesn't have a separate preprocessor, the directives we'll talk about 12 00:00:58,620 --> 00:01:02,130 in this lecture are processed as if there were one. 13 00:01:02,670 --> 00:01:09,360 We can recognize preprocessor directives by the fact that they start with the hash symbol preprocessor 14 00:01:09,360 --> 00:01:12,630 directive must be the only code in a line. 15 00:01:13,320 --> 00:01:17,850 Let's see some of the most useful preprocessor directives in C-sharp. 16 00:01:18,420 --> 00:01:25,440 In the last lecture, we mentioned the if debug and if released directives would allow us to include 17 00:01:25,440 --> 00:01:30,480 some called into compilation only if you are in debug or release mode. 18 00:01:31,140 --> 00:01:38,850 When writing preprocessor directives, we can use hash if hash elif and Hash Alice directives to control 19 00:01:38,880 --> 00:01:40,470 what we want to compile. 20 00:01:40,950 --> 00:01:45,930 Logically, they work the same as the if else, if and other statements. 21 00:01:46,470 --> 00:01:53,970 Besides checking for debug or release mode, we can also take things like the version of dotNet or the 22 00:01:53,970 --> 00:01:56,910 UP target, like iOS or Android. 23 00:01:57,510 --> 00:01:59,190 Let's see this in code. 24 00:02:38,360 --> 00:02:45,350 As you can see here, we built a pretty complex logic, depending on the build mode and the application 25 00:02:45,350 --> 00:02:46,070 target. 26 00:02:46,550 --> 00:02:53,960 Those directives are very useful when we build an application that targets more than one version or 27 00:02:53,990 --> 00:02:56,420 easements to work on different platforms. 28 00:02:57,910 --> 00:03:04,990 Another commonly used preprocessor directive is Hot Region Region allows us to define our region of 29 00:03:04,990 --> 00:03:07,960 code that can be collapsed in Visual Studio. 30 00:03:08,050 --> 00:03:11,500 So it doesn't affect the actual compilation process. 31 00:03:16,890 --> 00:03:19,530 And now we can collapse those lines. 32 00:03:24,460 --> 00:03:30,730 Returns are often used without the generated code that we don't really want to read that often. 33 00:03:31,390 --> 00:03:38,440 Some developers defined regions large files, so some parts of them can be collapsed, making the file 34 00:03:38,440 --> 00:03:40,570 seem smaller and easier to read. 35 00:03:40,960 --> 00:03:48,550 I wouldn't recommend that as this is simply sweeping the problem of a large class under the rug refactoring 36 00:03:48,550 --> 00:03:50,680 is definitely a better approach. 37 00:03:52,720 --> 00:04:00,360 We can also use error and wording we process or directives to explicitly create a compliance warning 38 00:04:00,370 --> 00:04:05,890 or let's say we want to raise a warning when this method is used. 39 00:04:13,380 --> 00:04:17,010 And here is the warning we created the same way. 40 00:04:17,040 --> 00:04:22,170 We could also create errors that would include the compensation process to finish. 41 00:04:24,000 --> 00:04:25,470 Speaking of warnings. 42 00:04:25,680 --> 00:04:32,640 We can disable some of them in a file using another preprocessor directive pragma warning disable. 43 00:04:33,240 --> 00:04:40,110 Let's consider this code, as we learned in the what is the difference between throw and throw x lecturer? 44 00:04:40,380 --> 00:04:47,850 Should rather you stroll instead of throw x, the compiler actually warns us, If we do this mistake, 45 00:04:48,420 --> 00:04:51,210 please pay attention to this warning code. 46 00:04:51,420 --> 00:04:53,370 US will need it in a second. 47 00:04:54,090 --> 00:05:01,110 If you are 100 percent sure what we are doing, we can disable this warning in the code using harsh 48 00:05:01,110 --> 00:05:02,640 pragma warning disable. 49 00:05:12,560 --> 00:05:19,940 As you can see, we must specify the type of warning by its code, and now the warning is gone from 50 00:05:19,940 --> 00:05:20,970 the warnings list. 51 00:05:21,650 --> 00:05:27,530 Pragma warning disable disables the warning until the end of the file, so it is a good practice to 52 00:05:27,530 --> 00:05:29,540 restore it after the last line. 53 00:05:29,570 --> 00:05:30,740 It should affect. 54 00:05:31,610 --> 00:05:32,330 All right. 55 00:05:32,720 --> 00:05:36,800 We worried about some, but not all preprocessor directives. 56 00:05:36,800 --> 00:05:37,700 Easy, sharp. 57 00:05:38,180 --> 00:05:43,880 As you can see, they can be pretty useful when it comes to controlling what code is compiled under 58 00:05:43,880 --> 00:05:47,780 given the circumstances or what warnings should be shown. 59 00:05:48,380 --> 00:05:54,680 If you want to read about all available pre-processed or directives, make sure to check out the article 60 00:05:54,680 --> 00:05:58,040 I linked in the resources attached to this lecture. 61 00:05:58,550 --> 00:06:02,870 In the next lecture will learn about one more preprocessor directive. 62 00:06:03,620 --> 00:06:09,440 If this topic comes up during the interview, you can be asked What is the preprocessor? 63 00:06:09,950 --> 00:06:17,150 A preprocessor, also known as the compiler, is a program that runs before the actual compiler, and 64 00:06:17,150 --> 00:06:21,620 it applies some operation on code before the compilation starts. 65 00:06:22,310 --> 00:06:30,110 How to disable selected warning in a file by using the pragma warning disable preprocessor directive, 66 00:06:30,470 --> 00:06:33,080 it takes the warning code as the parameter. 67 00:06:33,260 --> 00:06:40,460 So, for example, to disable the don't use strokes warning, we can write hush pragma warning disable 68 00:06:40,610 --> 00:06:44,900 see 22 zero zero as this is the code of this warning. 69 00:06:45,710 --> 00:06:48,020 All right, that's it in this video. 70 00:06:48,410 --> 00:06:51,140 Thanks for watching and see you in the next one.