Search code examples
c#asp.net-corerider

C# Implicit conversion of "someValue" from 'string' to 'StringValues' in Rider


Working with ASP.Net Core in Rider (learning it) and trying to set the Response headers. The headers work great, no problems. But in the Rider it adds some information (as on the image below) with the tip:

Implicit conversion of "someValue" from 'string' to 'StringValues'

enter image description here

What does this message mean?

Thanks.


Solution

  • C# has a mechanism for user to define an implicit conversion from one type to another i.e. usually (since types are validated by the compiler) given two unrelated user-defined types T1 and T2 the following T1 x = new T2(); will result in compiler error, but with implicit conversion defined compiler will use it and this code will work. See more in user-defined explicit and implicit conversion operators doc.

    context.Response.Headers is a IHeaderDictionary which has an indexer property of type StringValues which has an implicit conversion from string. The implicit conversion is defined like this:

    public static implicit operator StringValues(string? value)
    {
         return new StringValues(value);
    }
    

    Implicit conversions sometimes can be tricky (for example) so it seems that Rider team decided that it worth to notify user in some way when one is happening.