Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core: Array class does not contain definition for ConvertAll

I'm currently following https://www.hackerrank.com/challenges/diagonal-difference for my C# learning process. I copy and paste the given code for C#. Here I need to convert String array to Int Array. Example code uses Array.ConvertAll(a_temp,Int32.Parse) to do that. But in my Visual Studio Community 2017 IDE it gives error for ConverAll method.

'Array' does not contain definition for ConvertAll

But when I refered

  • 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

    it says it Exists(ConvertAll) in System namespace.

My IDE views

As you can see my IDE does not give suggestion for ConvertAll Method. I'm not sure this is a beginners dumb mistake. So I'll add my code in IDE here (Almost same code in 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);
        }
    }
}
like image 962
Menuka Ishan Avatar asked Mar 09 '23 07:03

Menuka Ishan


1 Answers

Looks like you are working in .NET Core project and there is not such method in .NET Core yet. You could either create new project with Full .NET Framework 4.6.x or use Enumerable Extensions to convert your array:

string[] a_temp = Console.ReadLine().Split(' ');
a[a_i] = a_temp.Select(s => Int32.Parse(s)).ToArray();
like image 161
Andrii Litvinov Avatar answered Apr 27 '23 23:04

Andrii Litvinov