Skip to main content
 首页 » 编程设计

c#之Int32 是日期时间吗

2024年02月05日6虾米姐

我正在编写一个控制台程序,我试图将 Console.ReadLine() 转换为 Int32,而不是键入 ReadLine() ,我不小心输入了 WriteLine()。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
 
namespace LinqIntegersDemo.cs 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            int[] nums = new int[10]; 
            var sorted = from c in nums orderby c descending select c; 
            int x = 0; 
 
            foreach (var n in nums) 
            { 
                Console.Write("Enter an integer >> "); 
                nums[x] = Convert.ToInt32(Console.WRITELINE()); 
            } 
        } 
    } 
} 

我(显然)得到了一个错误,但它没有说出预期的内容,而是说我无法将 void 转换为 System.DateTime。

为什么说 Int32 是 System.DateTime?

请您参考如下方法:

Convert.ToInt32 有一个重载接受:

  • 日期时间
  • bool 值
  • 字节
  • 查尔
  • 双人间
  • 十进制
  • Int16
  • Int64
  • 对象
  • 字符串
  • 还有很多,其他的请看文档

当它无法匹配任何重载时(void 不是有效选项)它只会选择在确定没有有效重载的步骤中失败的重载之一并使用在错误消息中。在本例中,它恰好选择了 DateTime 重载,但它也可以很容易地选择任何其他单参数重载。