为什么单步执行 C# 代码时属性代码块不会被命中?
我试图了解属性是如何工作的。我发现单步执行示例代码非常有帮助。但是,当我单步执行一个具有简单类和属性的小程序时,该属性永远不会受到影响。这让我想知道它是否被使用过。通过下面的代码,我可以看到类的私有变量被触及,但没有其他任何变化。我很困惑。另外,如果有人发现一个网站或视频是他们理解类属性的“啊哈”时刻,我很乐意看到它。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须修改默认调试器设置才能进入属性(
工具|选项 -> 调试 -> 常规
):You have to modify the default debugger settings to step into properties (
Tools|Options ->Debugging->General
):您应该检查 Visual Studio 中的设置:工具 ->选项->调试时,有一个选项:
确保未选中此选项。
You should check your settings in Visual Studio, in: Tools -> Options -> Debugging, there is the option:
Make sure this is unchecked.
在 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).