0

I am trying to translate a Java program to C#. In the Java program there is a line Arrays.fill(used, false);. When googled it, there is Array.Fill<>() in C#. But I am getting an error saying Array does not contain definition for Fill(). Does anybody know how to fix this?

My project is using .NET Framework 4.8.1. I am wondering why I cannot use Array.Fill() function documented in MSDN in my installation of Visual Studio.

My code is using the namespaces 'System', 'System.Collections.Generic', and 'System.Text'.

I have googled it and found one solution as adding 'System.Runtime.dll'. But I cannot find the file in 'ReferenceAssemblies' folder. I have downloaded and installed the .NET Framework from the website directly as well as tried repairing Visual Studio.

thomarjos
  • 29
  • 5

1 Answers1

2

This method is available in:

  • .NET: Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8
  • .NET Standard: 2.1

Check that your app targets one of above (not earlier .NET Framework 3.5/4.5 etc).

Dmitry
  • 16,110
  • 4
  • 61
  • 73
  • The app is targeting .NET Framework 4.8.1. – thomarjos May 05 '23 at 16:27
  • @thomarjos that means you can't use `Array.Fill` and should use the alternative from the dupe. – Guru Stron May 05 '23 at 16:37
  • @GuruStron But why is that? Isn't it part of the .NET Framework? – thomarjos May 05 '23 at 16:40
  • 1
    @thomarjos no. It is part of .NET (Core). See [this answer](https://stackoverflow.com/a/72240581/2501279) – Guru Stron May 05 '23 at 16:41
  • @GuruStron Thank you. That actually fixed it. I didn't which .NET I was supposed to use. I usually use .NET Framework project templates. It was not even mentioned in MSDN. You should write it as a solution so I can mark it. – thomarjos May 05 '23 at 17:00
  • @thomarjos Was glad to help! Accept this answer. It is pretty much saying the same - upgrade to .NET (Core). And upvote the answer I've linked) – Guru Stron May 05 '23 at 17:14