C# 字符串变量名日期在调试器中的行为异常
有人能告诉我为什么调试器将名为C# 字符串变量名日期在调试器中的行为异常,c#,.net,datetime,visual-studio-2015,C#,.net,Datetime,Visual Studio 2015,有人能告诉我为什么调试器将名为Date的string变量处理为DateTime对象 代码: 请参见屏幕截图: 使用.NET framework 4.5,VS-2015 谢谢 更新: 通过将代码缩减到尽可能小的程度,我发现了一个明显的问题 最小简化代码: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { DoSo
Date
的string
变量处理为DateTime
对象
代码:
请参见屏幕截图:
使用.NET framework 4.5,VS-2015
谢谢
更新:
通过将代码缩减到尽可能小的程度,我发现了一个明显的问题
最小简化代码:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DoSomething();
}
public static void DoSomething()
{
DateTime Date = DateTime.ParseExact("asdasd", "dd/MM/yyyy", CultureInfo.InvariantCulture);
}
public class HourRegistration
{
public string Date { get; set; }
}
}
}
截图:
在另一个与字符串完全相同的上下文中,它是一个不同的变量,调试器显示了另一个对象的详细信息(基于上下文)在调试模式下查找变量值时,它按名称匹配,而不是按内存地址匹配
我同意其他人的看法,可以做得更好,而且我在以前的版本(至少与2013年相比)中看到过这个问题。您将其设置为什么?您是否安装了自定义可视化工具?这是什么类型的应用程序?(如果你能在控制台应用程序中重现这一点,那将特别有趣。)我目前无法重现这一点-老实说,甚至不清楚上下文是什么-执行当前是否在
日期
getter中停止?其他地方?(我想知道它在看哪个实例…)我正在减少不必要的代码。调试某人的代码。尝试制作一个小型控制台应用程序来隔离问题。虽然我同意Visual Studio可以在这里做得更好,但这并不是什么新鲜事。如果您只需在手表窗口中键入“日期”,也会发生同样的情况。当前环境很重要。很有趣-我现在可以重新编程了。。。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DoSomething();
}
public static void DoSomething()
{
DateTime Date = DateTime.ParseExact("asdasd", "dd/MM/yyyy", CultureInfo.InvariantCulture);
}
public class HourRegistration
{
public string Date { get; set; }
}
}
}
//There in your above question you are creating a new object with datatype as datetime and variable as Date but this Date is not the one you described in your model.For that you have to do something like below:
HourRegistration model = new HourRegistration ();
model.Date = DateTime.ParseExact("asdasd", "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString();
//But this code gives an error since you cannot pass a string value to date.It makes no sense.