Tobias Knauss
Tobias Knauss

Reputation: 3509

Visual Studio: suppress output in immediate window

While testing my C# code, I manually assign different values to struct variables in the immediate window of Visual Studio 2019, like variable = new MyStruct().
This works well, but after every assignment the new value is printed. I assign a struct that has a few fields and a lot of public properties, so the immediate window prints the ToString() representation of the new struct and afterwards every single field and property of the struct, which make a total of 73 useless lines in the immediate window.

How can I suppress that output?
I can cheat by putting the assignment in braces (variable = new MyStruct()).Prop1 and querying a property of the struct, so I get only one line, which is much better, but that's not what I am looking for. I want no output at all, if possible, or at least the usual "expression has been evaluated and has no value.", because that one can be filtered out easily.

Upvotes: 1

Views: 217

Answers (1)

ΩmegaMan
ΩmegaMan

Reputation: 31616

Instead of the immediate window (which I love), how about decorating the struct/class using the DebuggerDisplay above the classes in question to give you just the live stats in the Watch windows?

a total of 73 useless lines in the immediate window.

[DebuggerDisplay("{" + nameof(GetDebuggerDisplay) + "(),nq}")]
public class MyHugePropertyLadenClass 
{ 
   ...
    /// <summary>
    /// During debugging let us know what we are l@@king at. 
    /// </summary>
    /// <returns>The text to show.</returns>
    private string GetDebuggerDisplay()    // Make this public if not shown in immediate
        => $"Viable: {IsViable} Valid: {IsValid} Address: {LocalAddress}";

Also not about the Immediate window, if it applies for you...one can remove properties using

[DebuggerBrowsable(DebuggerBrowsableState.Never)]

for it prevents the property following it from appearing in the debug window for the class.

The above code I showed, is done in .Net Core so some features may be different. Check out DebuggerDisplayAttribute Class

Upvotes: 1

Related Questions