I do not understand what 'conditional' means for conditional breakpoints in Visual Studio. Maybe somebody can explain the following behavior?
When setting a conditional breakpoint (selecting 'Is true' in the popup) I expect it to behave in the same way as an expression within an "if" statement.
For example:
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(!!)
Case (3) is obviously a typo. If I place expression (3) in an "if" statement
if (x = 42) { /* ... */ }
the code would not compile.
The typo in (3) can be dangerous. I've placed a simple demo at GitHub demonstrating the issue.
When reading the MSDN docs on the subject this should not happen, right?
Thankful for any pointers.
Update: The code from GitHub
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; }
}
}
The debugger works for debugging programs. Those programs may be written in any number of languages. F#, C#, VB, Assembly, C++, C, etc. Each of those languages has different semantics for it's ability to evaluate an expression true or not. The debugger really doesn't do anything very specific for a specific language (and in fact, probably doesn't even know a particular bit of code it gets a breakpoint on was written in any particular language). It really just knows where things are in memory, and what they represent.
Long story short, it doesn't abide by the C# syntax when evaluating expressions for conditional breakpoints.