-3

A simple Parallel.ForEach do not want to be in the Library Class

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace UtilyTools
{
    public class Why
    {
        public void gluk()
        {
            var intList= new List<int> { 1, 2, 3};
            int notMatter=0;
            Parallel.ForEach(intList, (item) => notMatter+= item);
        }
    }
    // [...]

Gives me the old:

CS0103 C# The name Parallel does not exist in the current context

Project.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
  </PropertyGroup>
</Project>

As requested a screen shoot of the error: nb: using are not underline in red.

Picture of the issue

Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
  • 1
    What's the exact error? – Carcigenicate Sep 01 '17 at 12:03
  • 3
    Note that this is subject to concurrency problems (mutating and reading the same variable in multiple threads). – Caramiriel Sep 01 '17 at 12:05
  • @Caramiriel, Well the `Parallel` in the error message was lost when i translate the error in English . sorry – Drag and Drop Sep 01 '17 at 12:09
  • 2
    Informations that are still missing are : What version of .NET framework are you building against? What is your target platform ( differences between UWP, WP and others ) ? – mrogal.ski Sep 01 '17 at 12:11
  • 1
    @Downvoter, some times you feel like talk to op is useless as he vanished. If you happends to come back on this question, please note that the typo was left after making a MCVE with random name as variable. the please variable was not for you but for the compiler. – Drag and Drop Sep 01 '17 at 12:33
  • @YvetteColomb, the `CS0103` with an early variable error while renamin Variable, may it look like a typo in early. But not so "beginnerish" if the miss click creating the library happends days before you start migrating code in it. Making early comment and answer revolved around `list` and `intList`. – Drag and Drop Sep 03 '17 at 07:56
  • you tell me if you think its a evolution from charmerlon or edit information that ppl were asking. In fact looking for those information was they to solve the issue. – Drag and Drop Sep 13 '17 at 12:58

2 Answers2

3
Parallel.ForEach(list, (item) => please += item);

should be:

Parallel.ForEach(intList, (item) => please += item);

You made a typo on the variable name (since corrected).

Additionally, please += item is not thread safe - you should use Interlocked.Add instead.

Also, make sure your Target Framework is at least .NET Framework 4. Then, make 100% sure that this line is at the top of the file:

using System.Threading.Tasks;

If you are using .NET Standard then see @m.rogalski's answer.

mjwills
  • 23,389
  • 6
  • 40
  • 63
3

Since you're using .NetStandard library and not the .NetFramework you have to include System.Threading.Tasks.Parallel as a dependency in to your project.

mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
  • 1
    The issue was a simple missclick when creating the Library. It was not a intendend use of the `.NetStandard` library . – Drag and Drop Sep 01 '17 at 12:34
  • @DragandDrop I had the same issues few weeks ago, that's why I asked about these. You have to remember that .NET Core is more modular than the "old" .NET framework so you just have to check if there's an additional module/library for the things which were normally in mscorlib.dll – mrogal.ski Sep 01 '17 at 12:39
  • **Hint:** Either place a `using System.Threading.Tasks;` on top of your code and use `Parallel.ForEach(...)`, or declare `using static System.Threading.Tasks.Parallel;` and use `ForEach(...)` without the `Parallel.` prefix. – Matt Mar 03 '20 at 09:23