C# 如何修复最佳重载方法匹配有一些无效参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15703737/
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
How to fix the best overloaded method match has some invalid arguments?
提问by user2224223
I am getting the error in the title, what is wrong with the code? I think its a syntax error, but I'm not sure as I don't have much information on what the error actually means.
我收到标题中的错误,代码有什么问题?我认为这是一个语法错误,但我不确定,因为我没有太多关于错误实际含义的信息。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please Input Number of Rows you want to make in your pyrimid: ");
int num = int.Parse(Console.Read()); // error here
Console.WriteLine(num);// Just to check if it is getting the right number
Console.Read();//This is Here just so the console window doesn't close when the program runs
}
}
}
Edit:
编辑:
Just to Clarify, I want the code just to get a number from the user and then print the number the user inputted.
只是为了澄清,我希望代码只是从用户那里获取一个数字,然后打印用户输入的数字。
采纳答案by Ilya Ivanov
int.Parse
accepts a string as parameter. Use Console.ReadLine()
to get a string from user and then pass it into int.Parse
int.Parse
接受一个字符串作为参数。用于Console.ReadLine()
从用户获取字符串,然后将其传递给int.Parse
int num = int.Parse(Console.ReadLine());
Note that this will throw FormatException
if the user will enter something not recognisable as int
. If you are not sure, that the user will input a good number (I always don't), use TryParse
. Example is given below
请注意,FormatException
如果用户输入无法识别的内容,这将抛出int
。如果您不确定用户是否会输入一个好的数字(我总是不这样做),请使用TryParse
. 示例如下
int value;
if (int.TryParse(Console.ReadLine(), out value))
Console.WriteLine("parsed number as: {0}", value);
else
Console.WriteLine("incorrect number format");
回答by VladL
The problem is that Console.Read() returns an int, but int.Parse is expecting a string. Simply change it to
问题是 Console.Read() 返回一个 int,但 int.Parse 需要一个字符串。只需将其更改为
int num =Console.Read();
回答by Haedrian
The reason you're getting that, is because Console.Read returns an int
你得到它的原因是因为 Console.Read 返回一个 int
http://msdn.microsoft.com/en-us/library/system.console.read.aspx
http://msdn.microsoft.com/en-us/library/system.console.read.aspx
And it can't parse an int, it can only parse strings.
它不能解析 int,它只能解析字符串。
You probabily want Console.ReadLine - which returns a string.
您可能需要 Console.ReadLine - 它返回一个字符串。
回答by Daniel Imms
Console.Read()
and ASCII
Console.Read()
和 ASCII
This is occurring because Console.Read()
actually returns and int
, not a string
. It returns the ASCII code of the key that was pressed, you will need to convert it to a char then to a string and then parse it.
发生这种情况是因为Console.Read()
实际上返回的是 and int
,而不是 a string
。它返回按下的键的 ASCII 代码,您需要将其转换为字符,然后转换为字符串,然后解析它。
var val = int.Parse(((char)Console.Read()).ToString());
Note that Console.Read()
will not return the integer in the format you think, values 0
to 9
actually come out at 60
to 70
as they're the key codesnot the characters you pressed.
请注意,Console.Read()
不会在你认为的格式返回整,价值观0
,以9
实际的出来60
,以70
作为他们的键码不是你按下的字符。
Console.ReadLine()
Console.ReadLine()
An alternative and probably better solution would be to use Console.ReadLine()
which returns a string
另一种可能更好的解决方案是使用Console.ReadLine()
which 返回一个string
var val = int.Parse(Console.ReadLine());
Warning
警告
You should always be careful when using int.Parse()
as it will throw an exception if the string provided is not numeric. A better option is to use int.TryParse()
which you give an out
argument and it returns whether parsing was successful.
使用时应始终小心,int.Parse()
因为如果提供的字符串不是数字,它将引发异常。更好的选择是使用int.TryParse()
您给出的out
参数,它返回解析是否成功。
string text = Console.ReadLine();
int val;
if (int.TryParse(text, out val))
{
// It is a number
}
{
// It is not a number
}