Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:27:49+00:00 2026-06-07T09:27:49+00:00

I want to copy Listbox items to StringCollection. If the listbox items contains empty

  • 0

I want to copy Listbox items to StringCollection.
If the listbox items contains empty string then ignore

Why can i do this:

foreach (string item in lstModelUsers.Items)
{
    if (string.IsNullOrEmpty(item))
        continue;
    else
        Options.Default.ModelRemoveUsers.Add(item);
}

BUT not this:

foreach (string item in lstModelUsers.Items)
    string.IsNullOrEmpty(item)
        ? continue
        : Options.Default.ModelRemoveUsers.Add(item);

Although both appear equal, the inline if statement generates a syntax error.
What is the best practice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:27:50+00:00Added an answer on June 7, 2026 at 9:27 am

    You can’t use the conditional operator like that. It only accepts expressions as its operands. Your code fails to compile because continue can only be used as a statement, not an expression.

    A better approach is to negate the if expression so that you don’t need the continue:

    foreach (string item in lstModelUsers.Items)
    {
        if (!string.IsNullOrEmpty(item))
        { 
            Options.Default.ModelRemoveUsers.Add(item);              
        }
    }
    

    You could also use Where:

    var itemsToAdd = lstModelUsers.Items
        .Cast<string>()
        .Where(item => !string.IsNullOrEmpty(item));
    
    foreach (string item in itemsToAdd)
    {
        Options.Default.ModelRemoveUsers.Add(item);   
    }
    

    If you are lucky, you may even find that ModelRemoveUsers has an AddRange method, then you don’t need the loop at all:

    var itemsToAdd = lstModelUsers.Items
        .Cast<string>()
        .Where(item => !string.IsNullOrEmpty(item));
    
    Options.Default.ModelRemoveUsers.AddRange(itemsToAdd);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listbox where I want to copy and paste items within that
I want to copy listbox items from one form to another....actually in the 1st
I want to copy a file from \\Server\SharedFolder\NameOfFile.txt to \\Server2\SecondSharedFolder\NameOfFile - 1-10-2012.txt I can
I want to copy items from an array that are not 0 or @
I have 4 items in a ListBox and each item does a specific thing
I want to copy all the files, and copy all the folders, included empty
I want copy whole folder to another folder using Copy-Item . My source folder
I want to copy all iPhone address book contacts into an array and then
I want to copy csv data from different files and then store in a
I want to copy a data directory into my distribution dir. copy_tree does this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.