Hello.
In this video, we're going to talk about Question 49, what are preprocessor directives preprocessor
directives?
Help us control the compensation process from the level of the code itself.
You can choose if some part of the code will be compiled or not.
We can disable or enable some compensation warnings, or we can even check for the doesnAt version and
execute different code, depending on it.
But first, what is a pre process or a preprocessor also known as the pre compiler?
Is the program that runs before the actual compiler that can apply some operations on code before it's
compiled.
Although the C-sharp compiler doesn't have a separate preprocessor, the directives we'll talk about
in this lecture are processed as if there were one.
We can recognize preprocessor directives by the fact that they start with the hash symbol preprocessor
directive must be the only code in a line.
Let's see some of the most useful preprocessor directives in C-sharp.
In the last lecture, we mentioned the if debug and if released directives would allow us to include
some called into compilation only if you are in debug or release mode.
When writing preprocessor directives, we can use hash if hash elif and Hash Alice directives to control
what we want to compile.
Logically, they work the same as the if else, if and other statements.
Besides checking for debug or release mode, we can also take things like the version of dotNet or the
UP target, like iOS or Android.
Let's see this in code.
As you can see here, we built a pretty complex logic, depending on the build mode and the application
target.
Those directives are very useful when we build an application that targets more than one version or
easements to work on different platforms.
Another commonly used preprocessor directive is Hot Region Region allows us to define our region of
code that can be collapsed in Visual Studio.
So it doesn't affect the actual compilation process.
And now we can collapse those lines.
Returns are often used without the generated code that we don't really want to read that often.
Some developers defined regions large files, so some parts of them can be collapsed, making the file
seem smaller and easier to read.
I wouldn't recommend that as this is simply sweeping the problem of a large class under the rug refactoring
is definitely a better approach.
We can also use error and wording we process or directives to explicitly create a compliance warning
or let's say we want to raise a warning when this method is used.
And here is the warning we created the same way.
We could also create errors that would include the compensation process to finish.
Speaking of warnings.
We can disable some of them in a file using another preprocessor directive pragma warning disable.
Let's consider this code, as we learned in the what is the difference between throw and throw x lecturer?
Should rather you stroll instead of throw x, the compiler actually warns us, If we do this mistake,
please pay attention to this warning code.
US will need it in a second.
If you are 100 percent sure what we are doing, we can disable this warning in the code using harsh
pragma warning disable.
As you can see, we must specify the type of warning by its code, and now the warning is gone from
the warnings list.
Pragma warning disable disables the warning until the end of the file, so it is a good practice to
restore it after the last line.
It should affect.
All right.
We worried about some, but not all preprocessor directives.
Easy, sharp.
As you can see, they can be pretty useful when it comes to controlling what code is compiled under
given the circumstances or what warnings should be shown.
If you want to read about all available pre-processed or directives, make sure to check out the article
I linked in the resources attached to this lecture.
In the next lecture will learn about one more preprocessor directive.
If this topic comes up during the interview, you can be asked What is the preprocessor?
A preprocessor, also known as the compiler, is a program that runs before the actual compiler, and
it applies some operation on code before the compilation starts.
How to disable selected warning in a file by using the pragma warning disable preprocessor directive,
it takes the warning code as the parameter.
So, for example, to disable the don't use strokes warning, we can write hush pragma warning disable
see 22 zero zero as this is the code of this warning.
All right, that's it in this video.
Thanks for watching and see you in the next one.