"二进制运算符添加未对类型'System.String'和'System.String'"定义; - 真的吗? [英] "The binary operator Add is not defined for the types 'System.String' and 'System.String'." -- Really?
问题描述
当试图运行下面的代码:
表达式来; Func键<字符串,字符串>> stringExpression = Expression.Lambda< Func键<字符串,字符串>>(
Expression.Add(
StringParam中,
Expression.Constant(A)
)
新的List< ParameterExpression>(){} StringParam中
);
串AB = stringExpression.Compile()(B);
我得到在标题中引用的错误:二元运算符添加不为类型定义 System.String'和'System.String'。事实是否真的如此呢?显然,在C#中它的作品。做字符串s =A+B
在C#中的特殊语法糖,表达式编译器不会访问?
< DIV CLASS =h2_lin>解决方案
这是绝对正确的,是的。有没有这样的运营商 - C#编译器转换字符串+字符串
成调用 string.Concat
。 (这一点很重要,因为这意味着 X + Y + Z
可转换成 string.Concat(X,Y,Z)
避免了无谓创建中间字符串。
有一个看的文档的字符串运算符 - 仅 ==
和 =
的定义!由框架
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?
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.
这篇关于"二进制运算符添加未对类型'System.String'和'System.String'"定义; - 真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!