类型为"?"的值不能用作默认参数 | .net 6 与 .net 4.8



这段代码用于.net core 6:

WebCallResult<AddressInfo> CreateAddress(
string coin,
string walletId,
string label,
int chain = 0,
string gasPrice = default,
bool lowPriority = false,
CancellationToken cancellationToken = default(CancellationToken));

我想在.net framework 4.8中使用它
在.net框架4.8中,我出现了以下错误:

类型为"?"的值不能用作默认参数,因为没有到类型"字符串"的标准转换

如何修复此错误?


编辑:
此行出现错误:

string gasPrice = default,

问题是,在gasPrice = default中,您正在为字符串设置default,在<C#7.1没有将default定义为字符串,这样字符串的值将是?,而不是像null这样可接受的值。

出于特定原因(运行时版本(,您必须设置default(string)或手动将值设置为null。可以在此处找到对所有默认值的引用。

最新更新