I am getting this eror "an item with the same key has been already added"

Dineshkumar.S 456 Reputation points
2022-09-02T10:41:50.753+00:00

if (videos != null)
{
foreach(HtmlAgilityPack.HtmlNode video in videos)
{
linkCounter += 1;
video.InnerHtml = video.InnerHtml + "[Video - To play, press Ctrl+" + linkCounter.ToString() + "]";
dcLinks.Add(linkCounter, video); // error
}
}

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | .NET CLI
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2022-09-02T11:24:22.247+00:00

    Try another instruction:

    dcLinks[linkCounter] = video;

    However, if you do not want to replace the existing items, then maybe you should clear dcLinks and restart linkCounter from zero.

    Probably it is much better to use a List instead of Dictionary.

    It depends on details that are not explained.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.