Home
C#
Conditional Attribute
Updated Jan 25, 2022
Dot Net Perls
Conditional. In C#, a Conditional method is not always executed. It is compiled into the program only when certain symbols are defined with #define.
The Conditional attribute enables conditional compilation. It accepts a string argument—this is the symbol that must be defined for the method to be compiled.
Attribute
Obsolete
This example program contains a MethodA, which is conditional on the symbol PERLS. It also contains MethodB, which is conditional on the symbol DOT.
Note At the top of the program, you can see 2 preprocessor directives. The symbol PERLS is defined. The symbol DOT is not.
Note 2 The execution of the program shows that MethodA is executed, but MethodB is not.
Detail It isn't possible to use Conditional on methods that return a value. Methods must be used in the void context.
void
And This is because variables can be assigned to method return values. You can work around this limitation using parameters.
#define PERLS #undef DOT using System; using System.Diagnostics; class Program { static void Main() { MethodA(); MethodB(); } [Conditional("PERLS")] static void MethodA() { Console.WriteLine(true); } [Conditional("DOT")] static void MethodB() { Console.WriteLine(false); } }
True
Conditional List. It is possible to modify a collection in DEBUG mode. You can add strings or other values conditionally—when debugging your application, not when it's deployed.
List
Directive
Info The program defines an AddToArray method, which simply adds two elements to the array. It uses the Conditional("DEBUG") attribute.
Detail This means to only compile the method if DEBUG is defined. The Conditional attribute requires System.Diagnostics.
using System; using System.Collections.Generic; using System.Diagnostics; class Program { static void Main() { List<string> l = new List<string>(); l.Add("rabbit"); l.Add("bird"); AddToArray(l); Console.WriteLine("Elements: {0}", l.Count); } [Conditional("DEBUG")] static void AddToArray(List<string> l) { // New array string[] a = new string[2]; a[0] = "cat"; a[1] = "mouse"; // Add array to end of list l.AddRange(a); } }
Elements: 4
Elements: 2
You can encapsulate your DEBUG-specific logic in separate Conditional("DEBUG") methods. This improves code clarity and performance, and reduces complexity.
Summary. Conditional allows us to create a method whose existence depends on a preprocessor directive. The Conditional attribute fits with the clear syntax of the C# language.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jan 25, 2022 (edit).
Home
Changes
© 2007-2025 Sam Allen