运算符“operator”无法应用于“type”和“type”类型的操作数
对不支持二元运算符的数据类型应用该运算符。 例如,无法在字符串中使用
||
运算符,无法在
bool
变量中使用
+
、
-
、
<
或
>
运算符,并且不能配合使用
==
运算符和
struct
类型(除非该类型显式重载该运算符)。
可重载运算符,使其支持某些类型的操作数。 有关详细信息,请参阅
运算符重载
。
在下例中,在 3 个位置生成 CS0019,原因是 C# 中的
bool
不能转换为
int
。将减法运算符
-
应用于字符串时,也会生成 CS0019。 加法运算符
+
可与字符串操作数一起使用,因为
String
类会重载该运算符以执行字符串串联。
static void Main()
bool result = true;
if (result > 0) //CS0019
// Do something.
int i = 1;
// You cannot compare an integer and a boolean value.
if (i == true) //CS0019
//Do something...
string s = "Just try to subtract me.";
float f = 100 - s; // CS0019
在以下示例中,必须在 ConditionalAttribute 外指定条件逻辑。 只能向 ConditionalAttribute 传递一个预定义符号。
以下示例生成 CS0019:
// CS0019_a.cs
// compile with: /target:library
using System.Diagnostics;
public class MyClass
[ConditionalAttribute("DEBUG" || "TRACE")] // CS0019
public void TestMethod() {}
// OK
[ConditionalAttribute("DEBUG"), ConditionalAttribute("TRACE")]
public void TestMethod2() {}
C# 运算符
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:https://aka.ms/ContentUserFeedback。
提交和查看相关反馈