Convert.ToDateTime(布尔)的要点是什么?

编程入门 行业动态 更新时间:2024-10-15 22:29:51
本文介绍了Convert.ToDateTime(布尔)的要点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在做某种类型的转换例程昨晚的系统我工作。其中一个转换涉及转字符串值到他们的DateTime等效。

I was doing some type conversion routines last night for a system I am working on. One of the conversions involves turning string values into their DateTime equivalents.

虽然这样做,我注意到Convert.ToDateTime()方法有它接受一个布尔值参数的重载。

While doing this, I noticed that the Convert.ToDateTime() method had an overload which accepted a boolean parameter.

第一个问题?在什么情况下会这样永远是有用的?

First question? Under what circumstances could this ever be useful?

我去远一点,并试图在快速监视执行的方法。无论哪种方式(true或false),则返回一个InvalidCastException。

I went a little further and tried to execute the method in QuickWatch. Either way ( true or false ), the routine returns an InvalidCastException.

第二个问题吗?为什么这个方法即使在这里?

Second question? Why is this method even here?

修改

感谢您的答案,伙计们。我可以看到它的有道理的从一个契约点,但它似乎奇怪,核心框架包含的方法: -

Thanks for the answers, guys. I can see how it makes sense from a contractual point of view, but it does seem odd that the core framework includes methods that:-

  • 永远不能工作
  • 更糟的是,实际上将抛出一个异常,当有人试图调用它。

这是一个有点像一个人做装有控制,主动地使用时,工作车辆停一辆车。

It's a bit like someone making a car loaded with controls that actively stop your vehicle from working when used.

推荐答案

这是有道理的,因为 ToDateTime 是一部分的 IConvertible 按布尔实现的接口。如果你看看在反射器,你会看到,它抛出一个 InvalidCastException的。

It makes sense because ToDateTime is part of the IConvertible interface implemented by bool. If you look in reflector you will see that it throws an InvalidCastException.

更新(从转换):

public static DateTime ToDateTime(bool value) { return ((IConvertible) value).ToDateTime(null); }

更多推荐

Convert.ToDateTime(布尔)的要点是什么?

本文发布于:2023-11-25 00:51:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627640.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔   要点   Convert   ToDateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!