为什么单步执行 C# 代码时属性代码块不会被命中?

发布于 2024-12-07 11:13:01 字数 954 浏览 0 评论 0原文

我试图了解属性是如何工作的。我发现单步执行示例代码非常有帮助。但是,当我单步执行一个具有简单类和属性的小程序时,该属性永远不会受到影响。这让我想知道它是否被使用过。通过下面的代码,我可以看到类的私有变量被触及,但没有其他任何变化。我很困惑。另外,如果有人发现一个网站或视频是他们理解类属性的“啊哈”时刻,我很乐意看到它。

using System;

public class Customer
{
    private int m_id = -1;

    public int ID
    {
        get
        {
            return m_id;
        }
        set
        {
            m_id = value;
        }
    }

    private string m_name = string.Empty;

    public string Name
    {
        get
        {
            return m_name;
        }
        set
        {
            m_name = value;
        }
    }
}

public class CustomerManagerWithProperties
{
    public static void Main()
    {
        Customer cust = new Customer();

        cust.ID = 1;
        cust.Name = "Amelio Rosales";

        Console.WriteLine(
                "ID: {0}, Name: {1}",
                cust.ID,
                cust.Name);

        Console.ReadKey();
    }
}

谢谢!

I'm trying to understand how Properties work. I've found that stepping though sample code can be very helpful. But When I step through a small program with a simple class and Property, the Property never gets hit. Which makes me wonder if its even being used. With the code below I can see that the private variables of the class are touched but nothing else. I'm confused. Plus if anyone has found a site or video that was their "ah hah" moment for understanding class properties I'd love to see it.

using System;

public class Customer
{
    private int m_id = -1;

    public int ID
    {
        get
        {
            return m_id;
        }
        set
        {
            m_id = value;
        }
    }

    private string m_name = string.Empty;

    public string Name
    {
        get
        {
            return m_name;
        }
        set
        {
            m_name = value;
        }
    }
}

public class CustomerManagerWithProperties
{
    public static void Main()
    {
        Customer cust = new Customer();

        cust.ID = 1;
        cust.Name = "Amelio Rosales";

        Console.WriteLine(
                "ID: {0}, Name: {1}",
                cust.ID,
                cust.Name);

        Console.ReadKey();
    }
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风吹过旳痕迹 2024-12-14 11:13:01

您必须修改默认调试器设置才能进入属性(工具|选项 -> 调试 -> 常规):

在此处输入图像描述

You have to modify the default debugger settings to step into properties (Tools|Options ->Debugging->General):

enter image description here

故笙诉离歌 2024-12-14 11:13:01

您应该检查 Visual Studio 中的设置:工具 ->选项->调试时,有一个选项:

Step over properties and operators (Managed only)

确保未选中此选项。

You should check your settings in Visual Studio, in: Tools -> Options -> Debugging, there is the option:

Step over properties and operators (Managed only)

Make sure this is unchecked.

风吹雪碎 2024-12-14 11:13:01

在 Visual Studio 2010 中,默认设置是跳过属性。您可以在“工具”->“工具”中更改此行为。选项->一般->跳过属性和运算符(仅限托管)。

In Visual Studio 2010, the default is to step over properties. You can change this behavior in the Tools -> Options -> General -> Step over properties and operators (Managed only).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文