Skip to content

Add StringValues operator+ #52507

Open
Open
@benaadams

Description

@benaadams

Background and Motivation

Currently if appending a value to a StringValues you need to use the static Concat method:

headers.ContentLanguage = StringValues.Concat(headers.ContentLanguage, requestCulture.UICulture.Name);

Overloading operator+ would allow it to be much cleaner

headers.ContentLanguage += requestCulture.UICulture.Name;

Proposed API

Please provide the specific public API signature diff that you are proposing. For example:

namespace Microsoft.Extensions.Primitives
{
    public readonly partial struct StringValues
    {
        public static StringValues operator +(StringValues left, StringValues right);
        public static StringValues operator +(string left, StringValues right);
        public static StringValues operator +(StringValues left, string right);
        public static StringValues operator +(string[] left, StringValues right);
        public static StringValues operator +(StringValues left, string[] right);
    }
}

Risks

Currently

headers.ContentLanguage += requestCulture.UICulture.Name;

Will compile; however it will implicitly convert the StringValues to a string add the two strings and then implictly convert back to a single value StringValue.

However; hopefully no-one is doing that as it will append the add in an incorrect way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions