Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading