Conditional
The Conditional
aspect allows methods to only be compiled and executed if a given conditional define is set. Different than conditionally compiling the method with an $IF
/#if
directive though, the method will be visible to code that calls it, regardless of whether the conditional define is set or not; if the define is not set, any calls to the method will simply be no-ops and not be emitted.
This makes Conditional
useful for methods that will be called from many places – without cluttering those call sites with directives.
A common scenario is to mark a DebugLog
method as conditional on the "DEBUG
" define. Calls to the method can then be added freely and without worry about overhead, as those calls will simply be eliminated when the define is not set (e.g. for Release builds).
[Conditional('DEBUG')]
method Log(aMessage: String);
begin
end;
[Conditional("DEBUG")]
public void Log(string message)
{
}
@Conditional("DEBUG")
public func Log(_ message: String!) {
}
@Conditional("DEBUG")
public void Log(string message)
{
}
Defined by the .NET Framework (on .NET) and by the the core compiler (other platforms).