Home > Net >  A value of type '?' cannot be used as a default parameter | .net 6 vs .net 4.8
A value of type '?' cannot be used as a default parameter | .net 6 vs .net 4.8

Time:01-07

This piece of code is for .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));

I want to use it in .net framework 4.8.
In .net framework 4.8 i have this error :

A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'string'

How can i fix this error?


EDIT :
Error occurs in this line :

string gasPrice = default,

CodePudding user response:

The problem is that in the gasPrice = default, you are setting a default for a string, which in <C#7.1 has no definition to default as a string, that way the value will be ? for string instead of a accepted value like null.

For that specific reason (the runtime version) you have to set the default(string)or manually set the value to null. References to all default values can be found here.

  •  Tags:  
  • Related