加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 字符串变量名称日期在调试器中很奇怪

发布时间:2020-12-15 07:52:46 所属栏目:百科 来源:网络整理
导读:有人能告诉我为什么调试器将名为Date的字符串变量处理为DateTime对象? 码: public class HourRegistration{ public string Date { get; set; }} 看屏幕截图: 使用.NET Framework 4.5,VS-2015 谢谢! 更新: 通过将代码减少到最小可能,我发现了明显的问题.
有人能告诉我为什么调试器将名为Date的字符串变量处理为DateTime对象?

码:

public class HourRegistration
{
    public string Date { get; set; }
}

看屏幕截图:

使用.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; }
        }
    }
}

截图:

它是另一个与字符串完全相同的上下文中的另一个变量,调试器显示了另一个对象的详细信息(基于上下文)

解决方法

//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",CultureInfo.InvariantCulture).ToString();

//But this code gives an error since you cannot pass a string value to date.It makes no sense.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读