Why does System.Convert has ToDateTime that accepts DateTime?

给你一囗甜甜゛ 提交于 2019-12-22 03:24:12

问题


//
// Summary:
//     Returns the specified System.DateTime object; no actual conversion is performed.
//
// Parameters:
//   value:
//     A date and time value.
//
// Returns:
//     value is returned unchanged.
public static DateTime ToDateTime(DateTime value);

Why does System.Convert has ToDateTime that accepts DateTime ?

The method documentation states the value remain unchanged.


回答1:


Convention, predictability and because the IConvertable defines the method ToDateTime.

My believe is that under the covers System.Convert simply runs through all the combinations of the IConvertable classes.




回答2:


The actual code is indeed:

public static DateTime ToDateTime(DateTime value)
{
  return value;
}

This confirms the value will be unmodified. There are a lot of convert methods. I think it is there just because it should accept all primitive objects in the signatures.

I.e. when using reflection, if would be unexpected if a convert from a DateTime to DateTime is not supported.



来源:https://stackoverflow.com/questions/21306667/why-does-system-convert-has-todatetime-that-accepts-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!