如何修复 "The * or -> operator must be applied to a pointer"?

How to fix "The * or -> operator must be applied to a pointer"?

我正在使用 Visual Studio 2019 学习 C# 中的赋值运算符,但由于错误代码 CS0193,脚本不是 运行。

首先我改变了“+”加号在“=”等价符号后面的位置,就像{c =+ a}一样,但它只适用于add和sub。我想使用 { c *= a} 但出现以下错误。

            int a = 21;

            int c =+ a;

            Console.WriteLine("1. Value of c is {0}", c );

            Console.WriteLine("2. Binery of c is {0}", Convert.ToString(c, 2));

            Console.ReadLine();

嗯,根据我的书,它应该像 ( x -= y ), (x += y), (x *= y) .​​.. 但在 Visual studio 2019 年的工作就像 (x =- y), (x =+ y), error, error …

错误代码:CS0193

说明:* 或 -> 运算符必须应用于指针。

运算符=结束,而不是以=开始。所以使用 *= += -= /=.

当您键入 int c =+ a 时,解析为:

int c = +a; // evuivalent to: int c = a

当您键入 int c =* a 时,解析为:

int c = *a; // error unless a is a pointer.

您正在混合声明和赋值。您只能在已声明的变量上使用 *=(以及 +=-=++)。所以你需要做类似的事情:

int a = 21;

int c = 1;

c *= a;