为什么我不能在out参数中传递更通用的对象?

时间:2017-08-25 18:04:08

标签: c# compiler-errors

为什么 c#编译器无法接受:

string propValue = "2017/12/01";
object propObjValue = null;
DateTime.TryParse(propValue, out propObjValue);

编译错误:参数2:无法从'out object'转换为'out System.DateTime'

然而它确实接受:

string propValue = "2017/12/01";
object propObjValue = null;
propObjValue = DateTime.Parse(propValue);

这是等效的,除了第二种方法我需要尝试/ catch以避免解析问题。

0 个答案:

没有答案