C# 如何在VisualStudio调试中显示条件的结果?
在通过调试时,如何让我在visual studio中显示条件的结果?假设我的代码如下所示:C# 如何在VisualStudio调试中显示条件的结果?,c#,vb.net,visual-studio,C#,Vb.net,Visual Studio,在通过调试时,如何让我在visual studio中显示条件的结果?假设我的代码如下所示: If a > b andalso c > b andalso d < y then 如果a>b且c>b且db isValid=isValid&&c>b isValid=isValid&&db 将cGTb设置为布尔值=c>b 尺寸dLTy为布尔值=db:{0}”,a>b); WriteLine(“c>b:{0}”,c>b); WriteLine(“db&&db&&c>b&&db&c>b&
If a > b andalso c > b andalso d < y then
如果a>b且c>b且d
如果我单步执行,我看不出这三个条件中哪一个是错误的。是否有一种方法可以?如果您想用最适合调试的方式编写代码,它将使用最糟糕的编码方式,例如,重写上述代码以进行调试的最佳方式如下:
bool isValid = false;
isValid = isValid && a > b
isValid = isValid && c > b
isValid = isValid && d < y
bool isValid=false;
isValid=isValid&&a>b
isValid=isValid&&c>b
isValid=isValid&&d
除非在你的程序中有某种逻辑来找出哪个部分出了故障。。。这种方式毫无意义,最好是将每个部分添加到手表中并对其进行验证,整体调试不是编写代码的目的。您可以使用debugconsole编写如下结果:
bool ab = a > b;
bool cb = c > b;
bool dy = d < y;
System.Diagnostics.Debug.WriteLine("a > b = " + ab + ", "c > b = " cb + ", d < y = " + dy");
if (ab && cd && dy)
{
//Your code here
}
也可以在控制台上打印。您可以使用即时窗口复制粘贴单个条件或其他任何您喜欢的内容。这样,您就不需要更改代码 如果我对代码有问题,我会像这样重写它,以便它清晰易懂,易于调试
Dim aGTb As Boolean = a > b
Dim cGTb As Boolean = c > b
Dim dLTy As Boolean = d < y
If aGTb AndAlso cGTb AndAlso dLTy Then
End If
Dim aGTb作为布尔值=a>b
将cGTb设置为布尔值=c>b
尺寸dLTy为布尔值=d
您可以使用系统诊断
并使用
Debugger.Break()
在此行停止调试器并查看调试器输出,如以下示例代码: 在C#中:
使用系统;
使用系统诊断;
课堂测试
{
静态易失性int a=a,b=2,c=3,d=4,y=5;
静态void Main(字符串[]参数)
{
Debugger.Break();
WriteLine(“a>b:{0}”,a>b);
WriteLine(“c>b:{0}”,c>b);
WriteLine(“db&&c>b&&db&&c>b&&db&c>b&d
或在VB中:
Imports System
Imports System.Diagnostics
Module Module1
Sub Main()
Dim a As Integer = 1, b As Integer = 2, c As Integer = 3, d As Integer = 4, y As Integer = 5
Debugger.Break()
Debug.WriteLine("a > b:{0}", a > b)
Debug.WriteLine("c > b:{0}", c > b)
Debug.WriteLine("d < y:{0}", d < y)
Debug.WriteLine("a > b && c > b && d < y:{0}", a > b AndAlso c > b AndAlso d < y)
If a > b AndAlso c > b AndAlso d < y Then
Console.WriteLine("...")
End If
End Sub
End Module
导入系统
导入系统。诊断
模块1
副标题()
尺寸a为整数=1,b为整数=2,c为整数=3,d为整数=4,y为整数=5
Debugger.Break()
Debug.WriteLine(“a>b:{0}”,a>b)
Debug.WriteLine(“c>b:{0}”,c>b)
Debug.WriteLine(“db&&c>b&&db和also c>b和also db且c>b且d
我希望这能有所帮助。试试这个:
if(a>b)
{
if(c>b)
{
if(d<y)
{
/* Your code here */
}
}
}
if(a>b)
{
如果(c>b)
{
如果(dHow)关于将a、b、c……的值复制到“监视”窗口。使用拖放。在这种情况下可能会起作用,但如果您遇到更复杂的条件,使用数组或调用函数,则很难理解其中哪些条件失败,而且很耗时。我希望会有工具提示或类似内容向我显示每个条件的结果。这就是为什么我们总是编写代码尽可能简单,这样每个人都可以毫不费力地阅读代码。并且始终避免快速和肮脏的代码。在“监视”窗口中添加以下表达式:a>b
、c>b
和d
。如果您有更复杂的express,您可能还需要添加表达式a>b&&c>b&&d离子,只需将它们添加到观察窗口。您最好这样做:bool condOne=a>b;bool condtow2=c>b;bool condtow3=d
类似的效果,但可读性更高。@CodyGray您是对的,但都会导致相同的结果……糟糕的代码;)您关于不需要更改代码的观点非常重要。调试不应干扰程序本身。立即窗口和监视窗口非常相似。监视窗口的一个优点是,每次执行中断时,那里的表达式都会自动更新。虽然此代码片段可以解决问题,但实际上它有助于请记住,你是在为将来的读者回答这个问题,那些人可能不知道你的代码建议的原因。
Imports System
Imports System.Diagnostics
Module Module1
Sub Main()
Dim a As Integer = 1, b As Integer = 2, c As Integer = 3, d As Integer = 4, y As Integer = 5
Debugger.Break()
Debug.WriteLine("a > b:{0}", a > b)
Debug.WriteLine("c > b:{0}", c > b)
Debug.WriteLine("d < y:{0}", d < y)
Debug.WriteLine("a > b && c > b && d < y:{0}", a > b AndAlso c > b AndAlso d < y)
If a > b AndAlso c > b AndAlso d < y Then
Console.WriteLine("...")
End If
End Sub
End Module
if(a>b)
{
if(c>b)
{
if(d<y)
{
/* Your code here */
}
}
}