与“int.Parse(string)”匹配的最佳重载方法有一些无效的参数
Console.WriteLine("Enter the page that you would like to set the bookmark on: ");
SetBookmarkPage(int.Parse(Console.ReadLine));
这是 int.Parse(string) 部分,为我提供了该线程主题的错误消息。不太明白我应该做什么,我正在将字符串解析为 int 并使用 SetBookmarkPage 方法发送它,我错过了什么? SetBookmarkPage 看起来像这样,并且包含在同一个类中:
private void SetBookmarkPage(int newBookmarkPage) {}
Console.WriteLine("Enter the page that you would like to set the bookmark on: ");
SetBookmarkPage(int.Parse(Console.ReadLine));
It's the int.Parse(string) part that gives me the error message of the topic of this thread. Don't really understand what I should do, I'm parsing a string into an int and sending it with the SetBookmarkPage method, what am I missing?
SetBookmarkPage looks like this and is contained in the same class:
private void SetBookmarkPage(int newBookmarkPage) {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不存在需要委托的
int.Parse
重载。听起来您想要这样做,但是,即使如此,您也会使您的程序面临潜在的异常。你应该做这样的事情:
There is no overload of
int.Parse
that takes a delegate. It sounds like you wanted to doHowever, even then you're exposing your program to a potential exception. You should do something like this:
将其更改为
Console.ReadLine 之后您缺少 ()
Change it to
You were missing () after Console.ReadLine
您需要调用 Console.ReadLine:
注意上面额外的
()
。您当前的方法正在传递从
Console.ReadLine
方法构建的委托,而不是正在调用的方法的结果。话虽这么说,如果您正在读取用户的输入,我强烈建议使用
int.TryParse
而不是int.Parse
。用户输入经常有错误,这可以让您优雅地处理它。You need to call Console.ReadLine:
Note the extra
()
in the above.Your current method is passing a delegate built from the
Console.ReadLine
method, not the result of the method being called.That being said, if you're reading input from a user, I would strongly recommend using
int.TryParse
instead ofint.Parse
. User input frequently has errors, and this would let you handle it gracefully.您想要:
目前它正在将
Console.ReadLine
视为方法组,并尝试应用方法组转换 - 如果您随后将其用作用于采用Func
或类似内容的方法的参数,但不适用于仅采用字符串的方法。您想要调用该方法,然后将结果作为参数传递。要调用该方法,您需要括号。
You want:
At the moment it's viewing
Console.ReadLine
as a method group, and trying to apply a method group conversion - which will work if you're then using it as an argument for a method taking aFunc<string>
or something similar, but not for a method taking just a string.You want to invoke the method, and then pass the result as an argument. To invoke the method, you need the parentheses.
您可能的意思是:
注意
ReadLine
之后的括号。您正在尝试传递 ReadLine 委托而不是返回值。You probably meant:
Notice the parens after
ReadLine
. You are trying to pass a delegate forReadLine
instead of the return value.Console.ReadLine
是一个方法,必须用括号调用它:没有括号,编译器认为它是一个方法组。
Console.ReadLine
is a method, you must invoke it with parenthesis:Without the parenthesis, the compiler thinks it is a method group.