“The binary operator Add is not defined for the types 'System.String' and 'System.String'.” — Really?

假装没事ソ 提交于 2019-12-10 12:33:24

问题


When trying to run the following code:

    Expression<Func<string, string>> stringExpression = Expression.Lambda<Func<string, string>>(
        Expression.Add(
            stringParam,
            Expression.Constant("A")
        ),
        new List<ParameterExpression>() { stringParam }
    );

    string AB = stringExpression.Compile()("B");

I get the error referenced in the title: "The binary operator Add is not defined for the types 'System.String' and 'System.String'." Is that really the case? Obviously in C# it works. Is doing string s = "A" + "B" in C# special syntactic sugar that the expression compiler doesn't have access to?


回答1:


It's absolutely right, yes. There is no such operator - the C# compiler converts string + string into a call to string.Concat. (This is important, because it means that x + y + z can be converted into string.Concat(x, y, z) which avoids creating intermediate strings pointlessly.

Have a look at the docs for string operators - only == and != are defined by the framework.




回答2:


This just caught me out too, and as Jon points out in his answer, the C# compiler converts string + string into string.Concat. There is an overload of the Expression.Add method that allows you to specify the "add" method to use.

var concatMethod = typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) }); 
var addExpr = Expression.Add(Expression.Constant("hello "),Expression.Constant("world"), concatMethod);

You might want to change the string.Concat method to use the correct overload.

Proving this works:

Console.WriteLine(Expression.Lambda<Func<string>>(addExpr).Compile()());

Will output:

hello world




回答3:


Yeah, it's a surprise isn't it!!! The compiler replaces it with a call to String.Concat.



来源:https://stackoverflow.com/questions/7027384/the-binary-operator-add-is-not-defined-for-the-types-system-string-and-syste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!