简体   繁体   English

我得到了 CS0029 C# 无法将类型“int”隐式转换为“bool”

[英]i got CS0029 C# Cannot implicitly convert type 'int' to 'bool'

im learning and i want to make so that if the age entered is 15 it will make a special message i just started yesterday so im trying to do random things to be sure i understand but i didnt learn that yet我正在学习,我想这样做,如果输入的年龄是 15 岁,它会发出一条特殊的信息,我昨天才开始,所以我尝试做一些随机的事情以确保我理解,但我还没有学到

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Meme
{
class Program
{
    static void Main(string[] args)
    {
        string yourName;
        Console.WriteLine("What is your name?");
        yourName = Console.ReadLine();
        Console.WriteLine("Hello {0}", yourName);
        Console.WriteLine("What is your age?");
        int yourAge = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Hello {0}, you are {1} years old", yourName, yourAge);
        Console.WriteLine("What is your rank?");
        int yourRank;
        yourRank = Convert.ToInt32(Console.ReadLine());
        const int Y = 15;
        if ((yourAge = Y))
        {
            Console.WriteLine("NO");
        }
    }
}

} }

C# uses == to mean "compare for equality" and = to mean "assign value to variable or property". C# 使用==表示“比较相等”,使用=表示“为变量或属性赋值”。 You've mixed them up.你把它们混在一起了。

Moreover, in C# (and many other languages), x = y is an expression that assigns the value of y to variable x and then produces the assigned value as a result .此外,在 C#(和许多其他语言)中, x = y是一个表达式,它将y的值赋给变量x ,然后产生所赋值的结果 So if (yourAge = Y) means "assign Y to yourAge , then use the integer value assigned to make the decision in the if ".因此, if (yourAge = Y)表示“将Y分配给yourAge ,则使用分配的整数值在if做出决定”。 But the condition of an if is required to be convertible to a bool , and int is not.但是if的条件需要可以转换为bool ,而int不是。 Hence the error.因此错误。

It would have been better to special-case this behaviour in the compiler so that accidental use of = instead of == produces a better error message.最好在编译器中对这种行为进行特殊处理,以便意外使用=而不是==会产生更好的错误消息。 C# does so in this situation, but only this situation: C# 在这种情况下会这样做,但仅限于这种情况:

bool b = whatever;
if (b = true)

There it says, sensibly, that you are probably using = where == is intended.在那里,明智地说,您可能正在使用= where ==的意图。 I wish we had made the compiler give this kind of error in more situations.我希望我们让编译器在更多情况下给出这种错误。 (Though if (b==true) is only marginally better; the right thing to type there is if (b) !) (虽然if (b==true)只是稍微好一点;正确的输入是if (b) !)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CS0029:无法将类型'int'隐式转换为'bool' - CS0029: Cannot implicitly convert type 'int' to 'bool' 错误 CS0029:无法将类型“bool”隐式转换为“(bool valorA, bool valorB)”c# - error CS0029: Cannot implicitly convert type 'bool' to '(bool valorA, bool valorB)' c# CS0029 C#无法将类型隐式转换为'string []' - CS0029 C# Cannot implicitly convert type to 'string[]' C# - 错误 CS0029 无法将类型“int”隐式转换为“Core.Models.Mandate” - C# - Error CS0029 Cannot implicitly convert type 'int' to 'Core.Models.Mandate' CS0029 C# 无法将类型隐式转换为“int?” ASP.NET CORE 5 MVC 标识 - CS0029 C# Cannot implicitly convert type to 'int?' ASP.NET CORE 5 MVC Identity 错误CS0029:无法将类型'int'隐式转换为'bool'-Unity3D - error CS0029: Cannot implicitly convert type `int' to `bool' - Unity3D 错误 CS0029:无法将类型“System.DateTime”隐式转换为“int”(CS0029)(DeclaringConstructor) - Error CS0029: Cannot implicitly convert type 'System.DateTime' to 'int' (CS0029) (DeclaringConstructor) 错误CS0029:无法将类型'int'隐式转换为'Score' - error CS0029: Cannot implicitly convert type 'int' to 'Score' 错误 CS0029:无法将类型“string”隐式转换为“int” - Error CS0029: Cannot implicitly convert type 'string' to 'int' 错误 CS0029:无法将类型“int”隐式转换为“string” - Error CS0029: Cannot implicitly convert type 'int' to 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM