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

  • Home
  • SEARCH
  • 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 156801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:23:28+00:00 2026-05-11T10:23:28+00:00

Why Cant I do something like this? If I have a List<String> myList populated

  • 0

Why Cant I do something like this?

If I have a List<String> myList populated with items, I want to be able to act on each member in a conditional way like so:

myList.ForEach(a => { if (a.Trim().Length == 0) a = '0.0'; }) 

But this will not compile. Im guessing its something to do with missing a return value?

Im trying to prepare a list of strings for conversion to doubles, and I want the empty items to show ‘0.0’ so I can just convert the whole list in one go.

  • 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. 2026-05-11T10:23:29+00:00Added an answer on May 11, 2026 at 10:23 am

    ForEach is not mutable, it doesn’t change the data structure in any way.

    You can either:

    1. Handle the loop yourself, with an index
    2. Produce a new list, using .Select and .ToList (provided you’re using .NET 3.5)

    Example of #1:

    for (Int32 index = 0; index < myList.Count; index++)     if (myList[index].Trim().Length == 0)         myList[index] = '0.0'; 

    With .NET 3.5 and Linq:

    myList = (from a in myList           select (a.Trim().Length == 0) ? '0.0' : a).ToList(); 

    With .NET 3.5, not using the Linq-syntax, but using the Linq-code:

    myList = myList.Select(a => (a.Trim().Length == 0) ? '0.0' : a).ToList(); 

    Edit: If you want to produce a new list of doubles, you can also do that in one go using Linq:

    List<Double> myDoubleList =     (from a in myList      select (a.Trim().Length == 0 ? '0' : a) into d      select Double.Parse(d)).ToList(); 

    Note that using ‘0.0’ instead of just ‘0’ relies on the decimal point being the full stop character. On my system it isn’t, so I replaced it with just ‘0’, but a more appropriate way would be to change the call to Double.Parse to take an explicit numeric formatting, like this:

    List<Double> myDoubleList =     (from a in myList      select (a.Trim().Length == 0 ? '0.0' : a) into d      select Double.Parse(d, CultureInfo.InvariantCulture)).ToList(); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use a double underscore in the keyword passed… May 12, 2026 at 12:52 am
  • Editorial Team
    Editorial Team added an answer Are security questions the best approach to this problem? Since… May 12, 2026 at 12:52 am
  • Editorial Team
    Editorial Team added an answer Something like: textBox1.Text = dt.Rows[0].ItemArray[0].ToString(); Depends on the name of… May 12, 2026 at 12:52 am

Related Questions

I am starting to use the jquery $.ajax() but I can't get back what
I'm stuck on a RegEx problem that's seemingly very simple and yet I can't
Why can't you pass an anonymous method as a parameter to the BeginInvoke method?
I'm really confused. I want to do something that a) seems like it should

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.