0

我不明白 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) 中的错字可能很危险。我在 GitHub 上放置了一个简单的演示来演示这个问题。

在阅读有关该主题的 MSDN 文档时,这不应该发生,对吧?

感谢任何指示。

更新:来自 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; }
        }
    }
4

1 回答 1

1

调试器用于调试程序。这些程序可以用任意数量的语言编写。F#、C#、VB、Assembly、C++、C 等。每种语言都有不同的语义,因为它能够评估表达式是否为真。调试器实际上并没有为特定语言做任何非常具体的事情(事实上,可能甚至不知道它获得断点的特定代码是用任何特定语言编写的)。它真的只知道事物在内存中的位置,以及它们代表什么。

长话短说,在评估条件断点的表达式时,它不遵守 C# 语法。

于 2012-09-09T18:22:12.733 回答