C#でデバッグ時にコードを有効にする方法を紹介します。
概要
デバッグ時のみコードを有効にするには #if #endif キーワードを利用します。
書式
#if DEBUG ~ #endif ブロック内のコードはデバッグ時のみコンパイルされ実行されます。リリース時には実行されません。~
#if DEBUG
(デバッグ時に有効なコード)
#endif
記述例
private void Button1_Click(object sender, EventArgs e)
{
#if DEBUG
StreamWriter sw = new StreamWriter(@"c:\log.txt",true, Encoding.ASCII);
sw.Write("Button1_Click\r\n");
sw.Close();
#endif
}
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2018-01-18
作成日: 2010-07-26