Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using NHunSpell Library.
Every time the loop run it overwrite the previous values in synonym2(array).
I don't want it to overwrite i want it to add onward.

What I have tried:

C#
List<string> synonym1 = new List<string>();
                string[] synonym2 = new string[50];
                foreach (ThesMeaning meaning in tr.Meanings)
                {
                    foreach (var synonym in meaning.Synonyms)
                    {
                        synonym1 = meaning.Synonyms.ToList();
                      
                    }
                    synonym2 = synonym1.ToArray();
                    Console.WriteLine(" Synonym:"+string.Join("\n", synonym2));
                }
Posted
Updated 11-Jun-16 2:00am
Comments
CHill60 11-Jun-16 7:08am    
You can't append to an array - use a list and just append the values. If you desperately need an array then convert to an array after both loops have finished
Danyal Awan 11-Jun-16 7:10am    
how to append in list
CHill60 11-Jun-16 7:13am    
Use the .Add() method of the List. List(T) Class (System.Collections.Generic)[^]

1 solution

Arrays aren't flexible - they can't increase and decrease in size once they are declared.
What I would do is replace the array with a List, but even if we do that you need to look fairly carefully at what exactly you are trying to do - that code is decidedly odd.
C#
foreach (var synonym in meaning.Synonyms)
{
    synonym1 = meaning.Synonyms.ToList();
}
Why do you have a loop here at all, given it just does the same thing every time you go round it?
Probably what you want to do is something like this:
C#
List<string> synonyms = new List<string>();
foreach (ThesMeaning meaning in tr.Meanings)
{
    synonyms.AddRange(meaning.Synonyms);
}
Console.WriteLine(string.Join("\n", synonyms.ToArray()));</string></string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900