Search code examples
c#.netdebuggingdebuggerdisplay

Is it possible to use conditions in a DebuggerDisplay?


Consider the following class:

[DebuggerDisplay("{GetType().Name,nq}: FileName = {FileName,nq}")]
public class FileWrapper
{
    public string FileName { get; set; }
    public bool IsTempFile { get; set; }
    public string TempFileName { get; set; }
}

I would like to add a debugger display based on the IsTempFileName property. I would like to add the string , TempFileName = {TempFileName,nq} when the instance is a temp file. How would I achieve something this?


Solution

  • You can use the conditional operator (?:)

    [DebuggerDisplay("{GetType().Name,nq}: FileName = {FileName,nq}{IsTempFile ? \", TempFileName: \" + TempFileName : System.String.Empty,nq}")]
    

    IsTempFile == false
    

    enter image description here


    IsTempFile == true
    

    enter image description here