comparing arrays c#

Hi:
I have 2 arrays, both string.
I need to store in a third array  those elements that are in array 1 but NOT in array 2.
I have some code that displays which is in BOTH, but that is not what I want!
Does anybody know how to change attached code to show what is in sfolders, but not dfolders?
Thanks!
List<string> miss = new List<string>();

foreach (string txt in dfolders)
{
   if (Array.IndexOf(sfolders, txt) == -1)
   miss.Add(txt);
 }

 

foreach (string item in miss)
{
    System.Diagnostics.Debug.Write(item);
    listBox4.DataSource = miss;
}

Open in new window

Richard TeasdaleFinancial ControllerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Asim NazirCommented:
May be this should work for you:
 
List<string> miss = new List<string>();

foreach (string txt in dfolders)
{
   if (Array.IndexOf(sfolders, txt) >= 0)
   miss.Add(txt);
 }

 

foreach (string item in miss)
{
    System.Diagnostics.Debug.Write(item);
    listBox4.DataSource = miss;
}

Open in new window

0
Dirk HaestProject managerCommented:
A very easy example provided below
List<string> firstList = new List<string>()
            {
	            "carrot",
	            "apple",
	            "banana"
            };

            List<string> secondList = new List<string>()
            {
	            "carrot",
	            "apple"
            };

            List<string> thirdArray = new List<string>();

            foreach (string item in firstList)
            {
                if (!secondList.Contains(item))
                    thirdArray.Add(item);
            }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carl TawnSystems and Integration DeveloperCommented:
Or, if LINQ is an option, then the simpler:
            List<string> firstList = new List<string>()
            {
	            "carrot",
	            "apple",
	            "banana"
            };

            List<string> secondList = new List<string>()
            {
	            "carrot",
	            "apple"
            };

            var unique = (from item in firstList.Except(secondList)
                          select item).ToArray();

            foreach (var item in unique)
                Console.WriteLine(item);

Open in new window

0
Richard TeasdaleFinancial ControllerAuthor Commented:
Thank you!
I do not know LINQ so cannot comment on that, but thanks to you all, anyway!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.