This is the code:
for (int i = 0; i < files.Count; i++)
{
if (pdf1.Lightnings.Count == 0)
{
pdf1.Lightnings.Add(files[i]);
}
if (files[i] != pdf1.Lightnings[i])
{
pdf1.Lightnings.Add(files[i]);
}
}
Both files and Lightnings are List<string>
For example in files I have 33 indexes (files names) and I want to add them to the Lightnings List.
But I want to check that if the file name from the List of files already exist in Lightnings so don’t add it again.
The way it is now I’m getting error since when the variable i=1 so the line:
if (files[i] != pdf1.Lightnings[i])
Throws an error since in Lightnings i have only one index [0] and in i=1 already
You can use the
.Containsmethod:This will check that
files[i]does not already exist in the collection before adding.