C# Preprocessor Directives Tutorial | Learn in 1 Day FREE
Last updated on 16th Aug 2022, Blog, Tutorials
Introduction
Processor directives give instruction to the compiler to preprocess the data before actual compilation starts.
All preprocessor directives start with #, and only white-space characters may appear before a preprocessor directive on a line.
Preprocessor directives are not statements, so do not end with a semicolon (;).
The directives are handled as if there were a preprocessor even if the C# the compiler lacks one.
In C# The preprocessor directives are used to help in the conditional compilation.
Unlike C and C++ directives, they are not used to make macros.
A preprocessor directive must be the only instruction on the line.
The Preprocessor
This concept belongs to the realm of the compiled languages.
A compiled language is a language which takes high-level code, like C# or C, and with the help of a compiler translate it to machine code.
The compiler is an abstraction layer between various architectures of manufacturers, and it knows how to translate blocks of code to different architectures like Intel or AMD processor instructions.
The preprocessor itself does not create the final machine code.
It is only preprocessing the code for the compiler.
It contains directives which are evaluated in advance and have a force on the compilation process.
Preprocessors are usually treated as separate entities from the compilers.
Depending on the language, preprocessors range from fairly simple to more difficult.
The preprocessor found in C# is considered quite easy, providing fewer capabilities than, for example, the one found in the C programming language.
This is the C# process flow that results in an executable application.
The Define Preprocessor
The #define preprocessor directive to generate symbolic constants. #define lets explain a symbol such that, by using the symbol as the expression passed to the #if directive, the expression evaluates to true.
- Its syntax is −
- #define symbol
- Example:
- #define PI
- using System;
- namespace PreprocessorDAppl {
- class Program {
- static void Main(string[] args) {
- #if (PI)
- Console.WriteLine(“PI is defined”);
- #else
- Console.WriteLine(“PI is not defined”);
- #endif
- Console.ReadKey();
- }
- }
- }
#undef directive
- The #undef directive permits us to undefine a symbol.
- Undefined symbols when used along with #if the directive will be estimated to false.
#if directive
- The #if directive is used to test preprocessor expression.
- A preprocessor expression may consists of a symbol only or combination of symbols along with the operators like && (AND), || (OR), ! (NOT).
- #if directive is followed by the #endif directive.
- The codes inside the #if directive is compiled only if the expression is tested with #if evaluates to true.
#elif directive
- The #elif directive is used along with #if directive that generates a compound conditional directive.
- It is used when testing multiple preprocessor expressions.
#else directive
- The #else directive is used along with the #if directive.
- If none of the expression in the preceding #if and #elif (if present) directives are true, the codes inside the #else directive will be compiled.
#endif directive
- The #endif directive is used along with the #if directive to indicate the end of #if directive.
#warning directive
- The #warning directive permits us to create a user-defined level one warning from our code.
#error directive
- The #error directive permits us to create a user-defined error from our code.
#line directive
- The #line directive allows us to change the line number and the filename for errors and warnings.
#region and #endregion directive
- The #region directive allows us to generate a region that can be expanded or collapsed when using a Visual Studio Code Editor.
- This directive is easily used to organize the code.
- The #region block cannot overlap with a #if block.
- , a #region block can be included within a #if block and a #if block can overlap with the #region block. #endregion directive indicates the end of the #region block.
#pragma directive
- The #pragma directive is used to give the compiler to special instructions for the compilation of the file in which it appears.
- The instruction may include disabling or enabling the warnings.
C# supports two #pragma instructions:
#pragma warning:
Used for disabling or enabling the warnings
#pragma checksum:
It creates checksums for source files which will be used for debugging.
Conditional Directives
Use the #if directive to generate conditional directive.
Conditional directives are helpful for checking a symbol or symbols to verify whether they evaluate to true.
If they do evaluate to true, the compiler evaluates all code between the #if and the next directive.
Syntax for conditional directive is
#if symbol [operator symbol]…
Where, symbol is the name of the symbol I want to test.
Can also use true and false or prepend the symbol with a negation operator.
The operator symbol represents the operator that is used to evaluate the symbol.
- Operators could be following −
- == (equality)
- != (inequality)
- && (and)
- || (or)
Group symbols and operators with the parentheses.
Conditional directives are used for compiling code for a debug builtor when compiling for a particular configuration A conditional directive start with a #if directive must explicitly be terminated with a #endif directive.
C# – Type Conversion
C# – Type Conversion
Type conversion is converting one type of data to another type.
It is also known as Type Casting.
In C#, type casting has 2 forms −
Implicit type conversion
These conversions are performed by the C# in a type-safe manner.
For example, are conversions from smaller to larger integral types and conversions from derived classes to the base classes.
Explicit type conversion –
These conversions are done explicitly by users by using the predefined functions.
Explicit conversions need a cast operator.
C# Type Conversion Methods
C# provides the following built-in type conversion methods are −
Sr.No | Methods & Description |
---|---|
1 | ToBoolean Converts a type to the Boolean value, where possible. |
2 | ToByte Converts a type to byte. |
3 | ToChar Converts a type to a one Unicode character, where possible. |
4 | ToDateTime Converts a type to date-time structures. |
5 | ToDecimal Converts a floating point or integer type to the decimal type. |
6 | ToDouble Converts a type to the double type. |
7 | ToInt16 Converts a type to the 16-bit integer. |
8 | ToInt32 Converts a type to the 32-bit integer. |
9 | ToInt64 Converts a type to the 64-bit integer. |
10 | ToSbyte Converts a type to the signed byte type. |
11 | ToSingle Converts a type to the small floating point number. |
12 | ToString Converts a type to the string. |
13 | ToType Converts a type to the specified type. |
14 | ToUInt16 Converts a type to the unsigned int type. |
15 | ToUInt32 Converts a type to the unsigned long type. |
16 | ToUInt64 Converts a type to the unsigned big integer. |
C# – Data Types
The variables in C#, are categorized into the following −
- Value types
- Reference types
- Pointer types
Value Type
Value type variables can be considered a value directly.
They are derived from a class System.ValueType.
The value types directly contain information.
Some examples are int, char, and float, which saves numbers, alphabets, and floating point numbers, respectively.
When declared an int type, the system allocates memory to save the value.
Reference Type
The reference types do not contain the actual data saved in a variable, but they contain a reference to the variables.
They refer to the memory location.
Using multiple variables, the reference types can refer to the memory location.
If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value.
For Example built-in reference types are: object, dynamic, and string.
Pointer Type
Pointer type variables save the memory address of another type.
Pointers in C# have the similar capabilities as the pointers in C or C++.
conclusion
How preprocessor imbue compiled languages with extra functionality that helps customize built and compilation processes.
Through basic examples, a use case was provided that demonstrated how these directives can be used, but how use this functionality will depend on the current application and business needs.
Are you looking training with Right Jobs?
Contact Us- Windows Azure Interview Questions and Answers
- Salesforce Architecture Tutorial
- Wrapper Class in Salesforce Tutorial
- salesforce lightning
Related Articles
Popular Courses
- VM Ware Training
11025 Learners
- Microsoft Dynamics Training
12022 Learners
- Siebel Training
11141 Learners
- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know