×

C# Tutorial

Basics of .Net

C# Basics

C# Fundamentals

C# Operators

C# Loops

C# Type Conversions

C# Exception Handling

C# String Class

C# Arrays

C# List

C# Stack

C# Queue

C# Collections

C# Character Class

C# Class and Object

C# Namespaces

C# Delegates

C# Constructors

C# Inheritance

C# Operator Overloading

C# Structures

C# File Handling

C# Convert.ToInt32()

C# Int32 (int) Struct

C# DateTime Class

C# Uri Class

C# Database Connectivity

C# Windows

C# Other Topics

C# Q & A

C# Programs

C# Find O/P

C# - #define Preprocessor Example

In this example, we are going to learn how to implement #define preprocessor using C# program?
Submitted by Nidhi, on September 11, 2020 [Last updated : March 22, 2023]

Here we will demonstrate the use of #define preprocessor in the C# program, here we will check defined macros to display messages on the console screen.

C# program to demonstrate the example of #define preprocessor

The source code to demonstrate the use of #define preprocessor is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# - #define Preprocessor Example

#define PRINT_MSG_TYPE1

using System;
class Program
{
    static void Main()
    {
        #if PRINT_MSG_TYPE1
            Console.WriteLine("Print message type1 on console screen");
        #endif

        #if PRINT_MSG_TYPE2
            Console.WriteLine("Print message type2 on console screen");
        #endif
    }
}

Output

Print message type1 on console screen
Press any key to continue . . .

Explanation

In the above program, we demonstrate the use of #define macro. Here we defined a macro PRINT_MSG_TYPE1 at the top in our program. Then we checked defined macros in the Main() method. Here we defined PRINT_MSG_TYPE1 macro but we did not define PRINT_MSG_TYPE2 macro that's why the message "Print message type1 on console screen" is printed.

C# Basic Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.