对于原语,我有一种意想不到的行为。
我的测试代码:
Nullable<long> value = long.Parse("5");
Type type = value.GetType();
// at this Point type is System.Int64 and not Nullable<System.Int64>有没有可能,如果value保持一个Nullable<System.Int64>而不被转换成一个常规的long
发布于 2021-01-24 14:06:17
当您查看文档 long.Parse返回long时,不会看到long?。
public static long Parse (string s);当然,可以将转换为long?,但这不会改变该语句。
long a = long.Parse("5");
Nullable<long> n = a; // convert to long?因此,简而言之,long.Parse绝不会让返回与long不同的东西。即使您提供了一些无效的输入:
long a = long.Parse("Hello"); //throws FormatException对变量调用runtime-type,,而不是编译时的GetType。当Parse在运行时返回一个long时,GetType当然也会返回。
发布于 2021-01-24 14:05:38
https://stackoverflow.com/questions/65871442
复制相似问题