Visual Studio में Debug मोड और Release मोड .NET प्रोजेक्ट को बिल्ड करने के लिए अलग-अलग कॉन्फ़िगरेशन हैं।
- Debug मोड: स्टेप-बाय-स्टेप डिबगिंग के लिए
- Release मोड: फाइनल असेंबली फाइल (.dll या .exe) के लिए ऑप्टिमाइज़्ड बिल्ड
बिल्ड कॉन्फ़िगरेशन बदलना
- Build मेनू → Configuration Manager → Debug या Release चुनें
- या टूलबार पर Solution Configurations ड्रॉपडाउन से चुनें
कंडीशनल कंपाइलेशन: #if DEBUG
#if DEBUG ब्लॉक के अंदर का कोड केवल तभी निष्पादित होता है जब प्रोजेक्ट Debug कॉन्फ़िगरेशन में बिल्ड हो।
class Program {
static void Main(string[] args) {
#if DEBUG
Console.WriteLine("Mode=Debug");
#else
Console.WriteLine("Mode=Release");
#endif
Console.ReadLine();
}
}
आउटपुट
// Debug मोड में:
Mode=Debug
// Release मोड में:
Mode=Release
कैसे काम करता है
DEBUGसिंबल Debug कॉन्फ़िगरेशन में डिफ़ॉल्ट रूप से परिभाषित होता हैTRACEदोनों कॉन्फ़िगरेशन्स में परिभाषित होता है- कस्टम सिंबल्स प्रोजेक्ट प्रॉपर्टीज → Build → Conditional compilation symbols में जोड़ सकते हैं
प्रोजेक्ट फाइल में कॉन्फ़िगरेशन
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
</PropertyGroup>
व्यावहारिक उपयोग के मामले
1. डिबग-ओनली लॉगिंग
public void ProcessData() {
#if DEBUG
Console.WriteLine($"Processing started at {DateTime.Now}");
#endif
// मुख्य लॉजिक
#if DEBUG
Console.WriteLine($"Processing completed at {DateTime.Now}");
#endif
}
2. अलग कनेक्शन स्ट्रिंग्स
string connectionString;
#if DEBUG
connectionString = "Server=localhost;Database=DevDB;...";
#else
connectionString = "Server=prod;Database=ProdDB;...";
#endif
3. डिबग-ओनली असर्शन
public void ValidateInput(string input) {
#if DEBUG
Debug.Assert(!string.IsNullOrEmpty(input), "Input cannot be null");
#endif
// प्रोडक्शन वैलिडेशन
if (string.IsNullOrEmpty(input)) throw new ArgumentException();
}
4. ConditionalAttribute (क्लीनर विकल्प)
[Conditional("DEBUG")]
public void DebugLog(string message) {
Console.WriteLine($"[DEBUG] {message}");
}
// Release बिल्ड में यह मेथड कॉल पूरी तरह हटा दिया जाता है
Release मोड ऑप्टिमाइज़ेशन्स
- कोड ऑप्टिमाइज़ेशन सक्षम
- डिबग सिंबल्स (PDB) वैकल्पिक
- इनलाइनिंग, डेड कोड एलिमिनेशन
- कम स्टैक ट्रेस जानकारी
चेतावनी
#if DEBUG में प्रोडक्शन लॉजिक न रखें - यह Release बिल्ड से पूरी तरह गायब हो जाता है।