我目前正在关注 https://www.hackerrank.com/challenges/diagonal-difference对于我的C#学习过程。我复制并粘贴了 C# 的给定代码。这里我需要将 String 数组转换为 Int 数组。示例代码使用 Array.ConvertAll(a_temp,Int32.Parse)
来执行此操作。但是在我的 Visual Studio Community 2017 IDE 中,它给出了 ConverAll 方法的错误。
'Array' does not contain definition for ConvertAll
但是当我提到
- https://msdn.microsoft.com/en-us/library/exc45z53(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.array(v=vs.110).aspx
它说它在系统命名空间中存在(ConvertAll)。
如您所见,我的 IDE 没有为 ConvertAll 方法提供建议。我不确定这是初学者的愚蠢错误。所以我将在此处的 IDE 中添加我的代码(在 hackerrank 中几乎是相同的代码)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int[][] a = new int[n][];
for (int a_i = 0; a_i < n; a_i++)
{
string[] a_temp = Console.ReadLine().Split(' ');
a[a_i] = Array.ConvertAll(a_temp, Int32.Parse);
}
}
}
最佳答案
看起来你在 .NET Core
项目中工作,而 .NET Core
中还没有这样的方法。您可以使用 Full .NET Framework 4.6.x
创建新项目或使用 Enumerable Extensions
来转换您的数组:
string[] a_temp = Console.ReadLine().Split(' ');
a[a_i] = a_temp.Select(s => Int32.Parse(s)).ToArray();
关于c# - .NET 核心 : Array class does not contain definition for ConvertAll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43534955/