【发布时间】:2012-08-02 21:35:41
【问题描述】:
我不明白“条件”对于 Visual Studio 中的条件断点意味着什么。也许有人可以解释以下行为?
在设置条件断点时(在弹出窗口中选择“为真”),我希望它的行为方式与“if”语句中的表达式相同。
例如:
int x = 1;
// (1) Breakpoint with expression "x == 1" returns True and the debugger stops here
// (2) Breakpoint with expression "x != 1" returns False and the debugger does not stop here
// (3) Breakpoint with expression "x = 42" returns ?? and the debugger stops here (!!) and executes the assignment(!!)
案例(3)显然是一个错字。如果我将表达式(3)放在“if”语句中
if (x = 42) { /* ... */ }
代码无法编译。
(3) 中的错字可能很危险。我已经放置了一个 simple demo at GitHub 来展示这个问题。
在阅读MSDN docs on the subject 时,这不应该发生,对吧?
感谢任何指点。
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
/// <summary>
/// Simple program showing that Visual Studio's conditional breakpoints do not have to return boolean results.
///
/// Tested with:
/// - Visual Studio 2005
/// - Visual Studio 2010
/// </summary>
public class Program
{
static void Main(string[] args)
{
var userService = new UserService();
var userJon = new User {Id = 1, Name = "Jon Doe"};
userService.SaveOrUpdateUser(userJon);
var userSally = new User { Id = 2, Name = "Sally Sample" };
userService.SaveOrUpdateUser(userSally);
foreach (var user in userService.Users) {
// IMPORTANT: Add a conditional breakpoint on the next line with 'user.id = 1' (instead of 'user.id == 1' or 'user.id.Equals(1)'). See doc folder for screenshots.
int id = user.Id;
// ...some logic...
user.Id = id;
// ...some more logic...
userService.SaveOrUpdateUser(user);
}
// Show results of what just happened on command line
Console.WriteLine("\n\nRESULTS==================================");
foreach (var user in userService.Users) {
Console.WriteLine("User Id: " + user.Id);
Console.WriteLine("User Name: " + user.Name);
}
Console.WriteLine("\n\nEND RESULTS==================================");
// Add a 'normal' breakpoint on the next line to read the console output
}
}
internal class UserService
{
public UserService()
{
Users = new List<User>();
}
public List<User> Users { get; private set; }
/// <summary>
/// Imagine this method doing a database insert or update!
/// </summary>
public void SaveOrUpdateUser(User user)
{
Console.WriteLine("\tSaveOrUpdateUser...");
Console.WriteLine("\tUser Id: " + user.Id);
Console.WriteLine("\tUser Name: " + user.Name);
User userAlreadyPresent = Users.FirstOrDefault(u => u.Name.Equals(user.Name)); // dummy find method
if (userAlreadyPresent == null) {
Console.WriteLine("Adding new user");
Users.Add(user);
}
else {
Console.WriteLine("\nUPDATING USER.......................");
Console.WriteLine("\tOld infos about user:");
Console.WriteLine("\tUser id: " + userAlreadyPresent.Id);
Console.WriteLine("\tUser name: " + userAlreadyPresent.Name);
Console.WriteLine("\tNew infos about user:");
Console.WriteLine("\tUser id: " + user.Id);
Console.WriteLine("\tUser name: " + user.Name);
userAlreadyPresent.Id = user.Id;
}
}
}
internal class User
{
public int Id { get; set; }
public string Name { get; set; }
}
}
【问题讨论】:
-
澄清一下,您是说如果您将
x = 42放入断点的条件字段中,它分配 42 的值给x变量当前在代码中执行 和 同时中断?这样,当您从该断点继续执行时,代码中的x现在将具有值 42? -
@ChrisSinclair 是的,就是这样。请尝试非常simple example I posted to GitHub
-
调试器不是编译器。
-
@Hans Passant ;-) 可能是真的?
-
@Chris Sinclair - 这正是 C/C++ 所做的,也是我期望它做的,即它被告知要做的。我很惊讶它无法编译,恕我直言,警告就足够了。毕竟你可以在 C# 中说
if (CallFunction())并期望函数在if中被调用。不执行代码是不一致的。
标签: c# visual-studio-debugging breakpoints conditional-breakpoint