简单控制台应用程序中的“无法将类型'string'隐式转换为'int'”错误

时间:2018-10-21 10:26:24

标签: c# .net-core

我在C#密码控件中收到此错误:

  

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

请您帮忙?

int newPassPP2 = 19;

Console.WriteLine("Enter the Password : ");

int readNewPassword = int.Parse(Console.ReadLine());

int writeNewPassword(int newNumOne)
{
    return newNumOne == readNewPassword ? "Number is true" : "Number is false";
}
Console.WriteLine(writeNewPassword(newPassPP2));

此代码行上发生错误:

return newNumOne == readNewPassword ? "Number is true" : "Number is false";

谢谢

2 个答案:

答案 0 :(得分:2)

您需要从本地方法返回一个字符串

   ...

   string writeNewPassword(int newNumOne)
   {
      return newNumOne == readNewPassword ? "Number is true" : "Number is false";
   }
   Console.WriteLine(writeNewPassword(newPassPP2));

   ...

答案 1 :(得分:1)

您的方法返回类型为 int ,现在将其更改为 string 的数据类型,

private string myMethod()