- Introduction to C# preprocessor directives
- Preprocessor Directives in C#
- Contingent Directives
- Nullable setting
- Contingent arrangement
- C# Preprocessor Importance
- Characterizing images
- Characterizing districts
- Blunder and cautioning data
- Scope of C# Preprocessor
- Pragmas
- Conclusion
- The preprocessor mandates give guidance to the compiler to preprocess the data before genuine gathering begins.
- All preprocessor mandates start with #, and just void area characters might show up before a preprocessor order on a line. Preprocessor orders are not articulations, so they don’t end with a semicolon (;).
- C# compiler doesn’t have a different preprocessor; notwithstanding, the orders are handled as though there was one. In C# the preprocessor orders are utilized to help in restrictive aggregation.
- Dissimilar to C and C++ mandates, they are not used to make macros. A preprocessor order should be the main guidance on a line.
Introduction to C# preprocessor directives :-
Preprocessor Directives in C# :
| ||
---|---|---|
1 | #define | It characterizes an arrangement of characters, called image. |
2 | #undef | It permits you to undefine an image. |
3 | #if | It permits testing an image or images to check whether they assess to valid. |
4 | #else | It permits to make a compound contingent order, alongside #if. |
5 | #elif | It permits making a compound contingent order. |
6 | #endif | Determines the finish of a restrictive order. |
7 | #line | It allows you to alter the compiler’s line number and (alternatively) the record name yield for mistakes and admonitions. |
8 | #error | It permits creating a mistake from a particular area in your code. |
9 | #warning | It permits creating a level one admonition from a particular area in your code. |
10 | #region | It allows you to determine a square of code that you can grow or fall while utilizing the illustrating element of the Visual Studio Code Editor. |
11 | #endregion | It denotes the finish of a #region block. |
- == (correspondence)
- != (imbalance)
- && (furthermore)
- || (or then again)
- #characterize DEBUG
- #characterize VC_V10
- utilizing System;
- public class TestClass {
- public static void Main() {
- #if (DEBUG && !VC_V10)
- Console.WriteLine(“DEBUG is characterized”);
- #elif (!DEBUG && VC_V10)
- Console.WriteLine(“VC_V10 is characterized”);
- #elif (DEBUG && VC_V10)
- Console.WriteLine(“DEBUG and VC_V10 are characterized”);
- #else
- Console.WriteLine(“DEBUG and VC_V10 are not characterized”);
- #endif
- Console.ReadKey();
- }
- #nullable incapacitate: Sets the nullable comment and cautioning settings to handicapped.
- #nullable empower: Sets the nullable comment and cautioning settings to empowered.
- #nullable reestablish: Restores the nullable comment and cautioning settings to project settings.
- #nullable incapacitate comments: Sets the nullable comment setting to impaired.
- #nullable empower comments: Sets the nullable comment setting to empowered.
- #nullable reestablish comments: Restores the nullable comment setting to project Settings.
- #nullable handicap admonitions: Sets the nullable admonition setting to debilitated.
- #nullable empower admonitions: Sets the nullable admonition setting to enabled.The preprocessor mandates give guidance to the compiler to preprocess the data before genuine gathering begins.
- All preprocessor orders start with #, and just void area characters might show up before a preprocessor mandate on a line. Preprocessor mandates are not proclamations, so they don’t end with a semicolon (;).
- C# compiler doesn’t have a different preprocessor; be that as it may, the mandates are handled as though there was one. In C# the preprocessor mandates are utilized to help in contingent assemblage. Dissimilar to C and C++ mandates, they are not used to make macros. A preprocessor order should be the main guidance on a line.
- #in the event that: Opens a restrictive aggregation, where code is accumulated provided that the predefined image is characterized.
- #elif: Closes the previous contingent arrangement and opens another restrictive aggregation in light of assuming the predetermined image is characterized.
- #else: Closes the first contingent assemblage and opens another restrictive gathering in the event that the past determined image isn’t characterized.
- #endif: Closes the first contingent aggregation.
- At the point when the C# compiler finds a #if mandate, followed in the long run by a #endif order, it aggregates the code between the orders provided that the predetermined image is characterized.
- Dissimilar to C and C++, you can’t allot a numeric worth to an image. The #if articulation in C# is Boolean and just tests whether or not the image has been characterized. For instance:
- #on the off chance that, alongside the #else, #elif, #endif, #define, and #undef mandates, allows you to incorporate or prohibit code in light of the presence of at least one images.
- Restrictive arrangement can be valuable while gathering code for an investigate construct or while ordering for a particular design.
- A restrictive order starting with a #if mandate should expressly be ended with a #endif mandate. #define allows you to characterize an image.
- By involving the image as the articulation passed to the #if mandate, the articulation assesses to valid.
- You can likewise characterize an image with the DefineConstants compiler choice. You can undefine an image with #undef.
- The extent of an image made with #define is the document wherein it was characterized. An image that you characterize with DefineConstants or with #define doesn’t struggle with a variable of a similar name.
- That is, a variable name shouldn’t be passed to a preprocessor mandate, and an image must be assessed by a preprocessor order.
- Versionless images are characterized no matter what the adaptation you’re focusing on.
- Adaptation explicit images are just characterized for the form you’re focusing on.
- The _OR_GREATER images are characterized for the adaptation you’re focusing on and every previous rendition. For instance, in the event that you’re focusing on .NET Framework 2.0, the accompanying images are characterized: NET_2_0, NET_2_0_OR_GREATER, NET_1_1_OR_GREATER, and NET_1_0_OR_GREATER.
- These are not quite the same as the objective structure monikers (TFMs) utilized by the MSBuild TargetFramework property and NuGet.
- C#
- Duplicate
- #characterize MYTEST
- utilizing System;
- public class MyClass
- {
- static void Main()
- {
- #if (DEBUG && !MYTEST)
- Console.WriteLine(“DEBUG is characterized”);
- #elif (!DEBUG && MYTEST)
- Console.WriteLine(“MYTEST is characterized”);
- #elif (DEBUG && MYTEST)
- Console.WriteLine(“DEBUG and MYTEST are characterized”);
- Console.WriteLine(“DEBUG and MYTEST are not characterized”);
- #endif
- }
- }
- C#
- Duplicate
- public class MyClass
- {
- static void Main()
- {
- #if NET40
- WebClient _client = new WebClient();
- #else
- HttpClient _client = new HttpClient();
- #endif
- }
- //…
- }
- #characterize: Define an image.
- #undef: Undefine an image.
- C#
- Duplicate
- #characterize VERBOSE
- #in the event that VERBOSE
- Console.WriteLine(“Verbose yield adaptation”);
- #endif
- #district: Start a locale.
- #endregion: End a district.
- #blunder: Generate a compiler mistake with a predefined message.
- #caution: Generate a compiler cautioning, with a particular message.
- #line: Change the line number printed with compiler messages.
- (1, 1): The beginning line and section for the main person on the line that follows the mandate. In this model, the following line would be accounted for as line 1, segment 1.
- (5, 60): The end line and section for the undeniable locale.
- 10: The section offset for the #line mandate to produce results. In this model, the tenth section would be accounted for as segment one. That is the place where the presentation int b = 0; starts. This field is discretionary. Whenever discarded, the order produces results on the principal section.\
- “incomplete class.g.cs”: The name of the result record.
- #pragma cautioning: Enable or impair admonitions.
- #pragma checksum: Generate a checksum.
- C#
- Duplicate
- // pragma_warning.cs
- utilizing System;
- #pragma cautioning incapacitate 414, CS3021
- [CLSCompliant(false)]
- public class C
- {
- int I = 1;
- static void Main()
- {
- }
- }
- #pragma cautioning reestablish CS3021
- [CLSCompliant(false)]//CS3021
- public class D
- {
- int I = 1;
- public static void F()
- {
- }
- }
Contingent Directives :-
You can utilize the #if mandate to make a contingent order. Restrictive mandates are helpful for testing an image or images to check in the event that they assess to valid.
Assuming that they really do assess to valid, the compiler assesses all the code between the #if and the following mandate.
Language structure for restrictive order is − #on the off chance that image [operator symbol]…
Where, image is the name of the image you need to test. You can likewise utilize valid and bogus or prepend the image with the invalidation administrator.
The administrator image is the administrator utilized for assessing the image.
Administrators could be both of the accompanying −
You can likewise bunch images and administrators with enclosures. Restrictive orders are utilized for accumulating code for a troubleshoot assemble or while arranging for a particular setup.
A contingent mandate starting with a #if order should expressly be ended with a #endif order.
The accompanying project shows utilization of restrictive orders −
Whenever the above code is incorporated and executed, it delivers the accompanying outcome − Troubleshoot and VC_V10 are characterized Albeit the compiler doesn’t have a different preprocessor, the mandates portrayed in this part are handled as though there were one. You use them to help in contingent arrangement. Not at all like C and C++ mandates, you can’t utilize these orders to make macros. A preprocessor mandate should be the main guidance on a line.
Nullable setting :-
The #nullable preprocessor order sets the nullable explanation setting and nullable admonition setting.This order controls whether nullable explanations have impact, and regardless of whether nullability admonitions are given. Every setting is either crippled or empowered.
The two settings can be indicated at the undertaking level (outside of C# source code).The #nullable order controls the explanation and cautioning settings and overshadows the undertaking level settings.A mandate sets the context(s) it controls until another order supersedes it, or until the finish of the source document.
The impact of the mandates is as per the following:
Contingent arrangement :-
You utilize four preprocessor mandates to control restrictive gathering:
Note :
C# Preprocessor Importance :-
For customary, non-SDK-style projects, you need to physically design the restrictive accumulation images for the different objective systems in Visual Studio through the task’s properties pages.
Other predefined images incorporate the DEBUG and TRACE constants. You can abrogate the qualities set for the undertaking utilizing #define. The DEBUG image, for instance, is consequently set contingent upon your fabricate arrangement properties (“Debug” or “Delivery” mode).The accompanying model tells you the best way to characterize a MYTEST image on a record and afterward test the upsides of the MYTEST and DEBUG images. The result of this model relies upon whether you constructed the task on Debug or Release setup mode.
The accompanying model tells you the best way to test for various objective structures so you can utilize fresher APIs whenever the situation allows:
Characterizing images :-
You utilize the accompanying two preprocessor orders to characterize or undefine images for restrictive aggregation:
You use #define to characterize an image. Whenever you utilize the image as the articulation that is passed to the #if order, the articulation will assess to valid, as the accompanying model shows:
Characterizing districts :-
You can characterize districts of code that can be imploded in a framework utilizing the accompanying two preprocessor mandates:
#district allows you to indicate a square of code that you can extend or fall while utilizing the illustrating element of the code supervisor. In longer code records, it’s helpful to implode or conceal at least one districts so you can zero in with respect to the document that you’re as of now chipping away at.
A #region block should be ended with a #endregion order. A #region block can’t cover with a #if block. Be that as it may, a #region square can be settled in a #if block, and a #if square can be settled in a #region block.
Blunder and cautioning data :-
You teach the compiler to create client characterized compiler mistakes and alerts, and control line data utilizing the accompanying mandates:
Scope of C# Preprocessor :-
The #line order may be utilized in a computerized, transitional advance in the form interaction.
For instance, assuming lines were eliminated from the first source code record, yet you actually needed the compiler to produce yield in view of the first line numbering in the document, you could eliminate lines and afterward reproduce the first line numbering with #line.
The #line concealed mandate conceals the progressive lines from the debugger, to such an extent that when the designer ventures through the code, any lines between a #line covered up and the following #line order (accepting that it is no other #line stowed away mandate) will be ventured over.
This choice can likewise be utilized to permit ASP.NET to separate between client characterized and machine-produced code. Despite the fact that ASP.NET is the essential purchaser of this component, all things considered, more source generators will utilize it. A #line stowed away order doesn’t influence document names or line numbers in mistake announcing.
That is, assuming the compiler observes a mistake in a secret square, the compiler will report the current record name and line number of the blunder. The #line filename order determines the record name you need to show up in the compiler yield.
As a matter of course, the real name of the source code record is utilized. The document name should be in twofold quotes (“”) and should be gone before by a line number.The parts of this structure are:
Pragmas :-
#pragma gives the compiler exceptional guidelines for the aggregation of the record in which it shows up. The guidelines should be upheld by the compiler. All in all, you can’t utilize #pragma to make custom preprocessing guidelines.
Note :
To track down notice numbers in Visual Studio, assemble your undertaking and afterward search for the notice numbers in the Output window.The handicap produces results starting on the following line of the source document. The admonition is reestablished on the line following the reestablish. On the off chance that there’s no reestablish in the record, the admonitions are reestablished to their default state at the main line of any later documents in a similar accumulation.
Conclusion :-
Preprocessor mandates are an incredible asset for eliminating code blocks from our gathering step.
Advancement stages like Xamarin, Mono, and even .NET itself have utilized preprocessor mandates to a serious level of progress. The most prescribed methodology is to modify our venture run setups, yet we have numerous choices, as may be obvious.
At long last, while this approach is strong, numerous designers ought to consider a system outside of arrangement, similar to include banners, on the off chance that they plan to flip conduct during runtime.