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 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

Related Questions

If I have something like a List[Option[A]] and I want to convert this into
I have a List of words I want to ignore like this one :
I have a grid in GXT, something like this: List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
I have a class like this that I want to save in ViewState for
I have a list of items, each of which contains a display property of
I cant call protected function in my base class. Why? It looks something like
I would like to do something like the following but can't seem to get
This is something I've always wondered, and I can't find any mention of it
I get the feeling this is probably something I should know but I can't
I have a RPC service with the following method: public List<Serializable> myMethod(TransactionCall call) {...}

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.