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

Dineshkumar.S 446 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
}
}

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,175 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
322 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,261 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.2K 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: Oldest