In Visual Studio, there are two modes for generating an application: Debug and release. How can we choose between them?
Assume that there is such a simple piece of code that calls Methods M1 and M1 in the main program to call the M2 method, m2 to call the M3 method, and M3 to throw an exception.
class Program
{
static void Main(string[] args)
{
M1();
Console.ReadKey();
}
static void M1()
{
M2();
}
static void M2()
{
M3();
}
static void M3()
{
throw new Exception("error");
}
}
Select the "debug" mode and place the breakpoint on the console. readkey (); line of code. Click "Debug> WINDOW> call stack" to display the "call stack" window information, as shown below:
Select the "release" mode and debug it again. The display is as follows:
From the stack information, we can see that debugging in debug and release modes consumes more memory, so it runs slowly. The release mode is optimized, it consumes less memory, so it runs faster.
In actual situations, you should deploy the applications generated in the release mode to the server, because the applications generated in the remease are optimized and compared to the applications generated in the debug mode, it runs faster.
In addition, if you want to run a piece of code only in debug mode, you can use the debug flag to write it as follows:
#if DEBUG
Console.WriteLine(DateTime.Now);
#endif
In general:
1. Compared with the debug mode, the release mode is more optimized and suitable for deployment to the server after the project is completed. The debug mode is more suitable for debugging.
2. The Code marked with debug is automatically deleted in release mode.