Articles → CSHARP → Conditional Attribute In C#
Conditional Attribute In C#
What Is Conditional Attribute?
Syntax
[Conditional("symbol_name")]
Example
// During development time
#define debug
using System;
using System.Diagnostics;
namespace Test {
class Program {
static void Main(string[] args) {
Program p = new Program();
p.ExecuteLogic1();
p.LogMessage("Test Message");
p.ExecuteLogic2();
Console.ReadLine();
}
[Conditional("debug")]
public void LogMessage(string message) {
Console.WriteLine(message);
}
public void ExecuteLogic1() {
Console.WriteLine("Logic 1");
}
public void ExecuteLogic2() {
Console.WriteLine("Logic 2");
}
}
}
Click to Enlarge
// During production deployment time
//#define debug
using System;
using System.Diagnostics;
namespace Test {
class Program {
static void Main(string[] args) {
Program p = new Program();
p.ExecuteLogic1();
p.LogMessage("Test Message");
p.ExecuteLogic2();
Console.ReadLine();
}
[Conditional("debug")]
public void LogMessage(string message) {
Console.WriteLine(message);
}
public void ExecuteLogic1() {
Console.WriteLine("Logic 1");
}
public void ExecuteLogic2() {
Console.WriteLine("Logic 2");
}
}
}
Click to Enlarge