c# - 错误1无法将类型'char'隐式转换为'bool'

标签 c# .net

我看过其他问题,但是答案似乎太复杂了,无法理解,所以这是我的代码:

注意:我是C Shar语言的新手

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

namespace Input_Program
{
    class Program
    {
       private static void Main()
       {
           Console.WriteLine("Welcome to my bool program!");
           Console.WriteLine("Input a NON capital y or n when told to.");


            char Y;
            char N;

            if(Y = 'y')
            {}
        }
    }
}


谢谢
谢谢

最佳答案

您缺少等号:if(Y == 'y')

现在,将值'y'分配给Y,这也是赋值表达式的结果。这是一个char,而编译器期望的是bool结果。

更新:

当然,如thephpdeveloper所说,编译器还将坚持要求您在读取Y之前为其分配一个值。所以:

char Y = 'y';
char N = 'n';

if(Y == 'y')
{}

关于c# - 错误1无法将类型'char'隐式转换为'bool',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5399972/

相关文章:

c# - 安全 DSA 签名

c# - MVC.net 2 - 更改 ValidationMessageFor 输出的 HTML - 这可以通过模板进行吗?

c# - 类型比较未返回预期结果

c# - 线程安全问题

c# - 如何更改文件扩展名的默认图标?

.net - ASP.Net MVC - 为什么要为 favicon.ico 创建 Controller ?

c# - 无法在 WPF C# 代码中获取对 png 作品的引用

c# - MVC4 模型验证然后使用 JQuery 将 JSON post 提交到外部 URI

c# - 在 C# 控制台应用程序中生成 HTML/XML 标记?

c# - 我如何重写此代码以避免 "Linq to entities Does not recognize method"错误