C# 无法将类型“string”隐式转换为“bool”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8895547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:01:47  来源:igfitidea点击:

Cannot implicitly convert type 'string' to 'bool'

c#

提问by user1152722

Possible Duplicate:
Help converting type - cannot implicitly convert type 'string' to 'bool'

可能的重复:
帮助转换类型 - 不能将类型“字符串”隐式转换为“布尔”

I've got this code:

我有这个代码:

private double Price;
private bool Food;
private int count;
private decimal finalprice;

public void Readinput()
{
    Console.Write("Unit price:  ");
    Price = Console.ReadLine();

    Console.Write("Food item y/n:  ");
    Food = Console.ReadLine();

    Console.Write("Count:  ");
    count = Console.ReadLine();
}

private void calculateValues()
{
    finalprice = Price * count;
}

and get the following errors:

并得到以下错误:

Cannot implicitly convert type 'string' to 'bool'

Cannot implicitly convert type 'string' to 'double'

Cannot implicitly convert type 'string' to 'int'

Cannot implicitly convert type 'double' to 'decimal'. An explicit conversion exists (are you missing a cast?)

无法将类型“string”隐式转换为“bool”

无法将类型“字符串”隐式转换为“双精度”

无法将类型“string”隐式转换为“int”

无法将类型“double”隐式转换为“decimal”。存在显式转换(您是否缺少演员表?)

I know what it means but I don't know how to fix it.

我知道这意味着什么,但我不知道如何解决它。

回答by adatapost

Use bool.Parseor bool.TryParsemethod to convert string value to boolean.

使用bool.Parsebool.TryParse方法将字符串值转换为boolean.

Price = double.Parse(Console.ReadLine());
Food =bool.Parse(Console.ReadLine());
count = int.Parse(Console.ReadLine());

You can't convert "y" or "n" value to boolean instead your have to receive value as a string and if it is "y" then store true, falseotherwise.

您不能将 "y" 或 "n" 值转换为布尔值,而必须将值作为字符串接收,如果它是 "y" 则 store truefalse否则。

Console.Write("Food item y/n:  ");
string answer = Console.ReadLine();
if(answer=="y")
   Food=true;
else
   Food=false;

Or (suggestion of @Mr-Happy)

或者(@Mr-Happy 的建议)

 Food = answer == "y"

You need to specify explicit cast while calculating finalprice.

您需要在计算时指定显式转换finalprice

private void calculateValues()
{
   // convert double result into decimal.
    finalprice =(decimal) Price * count;
}

回答by BlackBear

You have to convert what you read from the console (which is a string) to the actual type using the static class Convert. For example:

您必须使用静态类 Convert 将从控制台读取的内容(字符串)转换为实际类型。例如:

Console.Write("Count:  ");
count = Convert.ToInt32(Console.ReadLine());

This crashes if the argument given can't be converted, but this is not your primary problem right now, so let's keep it simple.

如果给定的参数无法转换,则会崩溃,但这不是您现在的主要问题,所以让我们保持简单。

回答by C-Pound Guru

Console.Write("Unit price:  ");
double.TryParse(Console.ReadLine(), out Price);

Console.Write("Food item y/n:  ");
bool.TryParse(Console.ReadLine(), out Food);

Console.Write("Count:  ");
int.TryParse(Console.ReadLine(), out count);

回答by Wouter Janssens

private double Price;
private bool Food;
private int count;
private decimal finalprice;

public void Readinput()
{
    Console.Write("Unit price:  ");
    double.TryParse(Console.ReadLine(), out Price);

    Console.Write("Food item y/n:  ");
    bool.TryParse(Console.ReadLine(), out Food);

    Console.Write("Count:  ");
    int.TryParse(Console.ReadLine(), out count);
}

private void calculateValues()
{
    finalprice = Price * count;
}

回答by Nuffin

You have to wrap the Console.ReadLine()calls in the appropriate parser functions, since (unlike PHP, for example) C# is a static typed language, additionally only conversions which are guaranteed to be both safe and lossless can be done implicitly:

您必须将Console.ReadLine()调用包装在适当的解析器函数中,因为(例如,与 PHP 不同)C# 是一种静态类型语言,此外,只有保证安全和无损的转换才能隐式完成:

Price = double.Parse(Console.ReadLine());

Console.Write("Food item y/n:  ");
// I think you want the user to type in "y", "Y", "n" or "N", right?
Food = Console.ReadLine().ToUpper() == "Y";

Console.Write("Count:  ");
count = int.Parse(Console.ReadLine());

And in your calculation method, you have to explicitly convert the resulting double into a decimal since C# doesn't support implicit conversion between fixed point and floating point values:

并且在您的计算方法中,您必须将结果双精度显式转换为小数,因为 C# 不支持定点值和浮点值之间的隐式转换:

finalprice = (decimal)(Price * count);