c# - Console.ReadLine()。ToString()生成错误

标签 c#

我从未进行过控制台编程,因此陷入了这一愚蠢的事情:

Console.Write("Enter customer's salary: ");
string sal = Console.Write("{0}! ", Console.ReadLine().ToString());


它产生错误:无法将类型'void'隐式转换为'string'

最佳答案

Console.Write是一个无效方法,它不返回其写入控制台的内容。
您实际需要的是string.Format

string sal = string.Format("{0}! ", Console.ReadLine());


另外,末尾的额外ToString()是冗余的(如注释中所述),其原因有两个:


Console.ReadLine已经返回一个字符串
字符串格式化功能接受对象作为参数,如果需要,它们会自动转换为字符串。

关于c# - Console.ReadLine()。ToString()生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4044233/

相关文章:

C#:当上下文菜单的菜单项链接到两个不同的对象时,如何检测谁是调用者?

c# - Linq to Entities 查询可空数据类型

c# - 使用区域和 Controller 名称进行路由(asp.net core)

c# - 更改 itextsharp 的字体大小

c# - 什么时候可以使用 ClearAllPools 方法?

c# - DropDownListFor 回调或 if 语句

c# - 更改 WP8.1 WebView 中的用户代理

c# - 在 MVC View 中引用内部类

c# - 属性 C# VS2010 的特定自动格式

c# - 我怎么知道某些扩展方法是否使用 'mutate' 对象?