UnityのTutorialなどでConditionalというキーワードを見かけた。
C#の機能らしい。
# define TRACE_ON
using System;
using System.Diagnostics;
public class Trace
{
[Conditional("TRACE_ON")]
public static void Msg(string msg)
{
Console.WriteLine(msg);
}
}
public class ProgramClass
{
static void Main()
{
Trace.Msg("Now in Main...");
Console.WriteLine("Done.");
}
}
TRACE_ONの#defineをコメントアウトするとTrace.Msg()が実行されなくなる。
上記のmicrosoftのリンクでは
Using Conditional is a cleaner, more elegant, and less error-prone alternative to enclosing methods inside ,
ただ、複数の条件をANDで使うときは、下記のようにコードが冗長になるような気がする。
[Conditional("A")]
static void DoIfA()
{
DoIfAandB();
}
[Conditional("B")]
static void DoIfAandB()
{
// Code to execute when both A and B are defined...
}
これは#if defined()を使う方がいいかも。