The #region
and #region
preprocessor directives are used in conjunction with Visual Studio's Code Editor. Let's work with the following example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDefine {
class Program {
static void Main(string[] args) {
//---implement ions here---
}
}
}
Often, you have many functions that perform specific tasks. In such cases, it is often good to organize them into regions so that they can be collapsed and expanded as and when needed. Using this example, you can group all the methods — Method1()
, Method2()
, and Method3()
— into a region using the #region and #region preprocessor directives:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDefine {
class Program {
static void Main(string[] args) {}
private void Method1() {
//---implementations here---
}
private void Method2() {
//---implementations here---
}
private void Method3() {
//---implementations here---
}
}
}
In Visual Studio 2008, you can now collapse all the methods into a group called 'Helper functions'
. Figure 3-19 shows the Code Editor before and after the region is collapsed.

Figure 3-19
The #region
and #region
preprocessor directives do not affect the logic of your code. They are used purely in Visual Studio 2008 to better organize your code.
#pragma warning
The #pragma warning
directive enables or disables compiler warning messages. For example, consider the following program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDefine {
class Program {
int num = 5;
static void Main(string[] args) {}
}
}
In this program, the variable num
is defined but never used. When you compile the application, the C# compiler will show a warning message (see Figure 3-20).

Figure 3-20
To suppress the warning message, you can use the #pragma warning
directive together with the warning number of the message that you want to suppress:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDefine {
class Program {
int num = 5;
static void Main(string[] args) {}
}
}
This example suppresses warning message number 414 ('The private field 'field' is assigned but its value is never used'). With the #pragma warning
directive, the compiler will now suppress the warning message (see Figure 3-21).

Figure 3-21
You can suppress multiple warning messages by separating the message numbers with a comma (,) like this: