1

I'd like to replace all null value in my List<string> but if don't want to do a foreach loop.

List<string> tmpList = new List<string>();
//src is a List<string> where I want to remplace the null by "NULL"
foreach(string s in src) 
{  
    if(s == null) 
    {
        tmpList.Add("NULL");
    }
    else

    {
        tmpList.Add(s);
    }
}
src = tmpList;

Do you know a better way to do this ? With LINQ may be ?

2
  • 3
    What's wrong with for (int i = 0; i < src.Count; i++) src[i] = src[i] ?? "NULL"; Commented May 19, 2014 at 12:09
  • What do you mean by "better"? Commented May 19, 2014 at 12:10

2 Answers 2

12
src.Select(s => s ?? "NULL").ToList();

But what's wrong with using a foreach loop?

Sign up to request clarification or add additional context in comments.

10 Comments

This uses also a loop and it creates a new List which is less efficient than a simple loop.
foreach internally, plus less inefficient than a simple loop.
I guess it depends on why a loop isn't desired. If performance isn't a concern and the OP just wants a single line of concise code, this is fine. (Note that the original code also creates a new list, leaving the original list unmodified. There could be a reason for that not being shared in the question.)
@EhsanSajjad s ?? "NULL" means: if s is not null, return s, otherwise return "NULL"
@ken2k: there's nothing more efficient than modifying the original list directly: for (int i = 0; i < src.Count; i++) src[i] = src[i] ?? "NULL";. No need for another list which doubles the memory temporarily.
|
1
var list = new List<string>() { null, "test1", "test2" };
for (int i = 0; i < list.Count; i++)
{
    if (list[i] == null)
    {
        list[i] = "NULL";
    }
}

No foreach.


EDIT:

As no one seems to understand the meaning of this answer: LINQ does a foreach loop internally. You want to conditionally modify each item of a list? Then you have to enumerate it.

LINQ is here to help us write queries. Oh wait, this is the Q of LINQ.

LINQ is NOT here to modify existing lists. Just use a good old for loop here. You could of course create a new list based on the existing one with modified values (see best-voted answer), but I'm afraid you'll start using LINQ in a wrong way.

10 Comments

If I were to guess... This still uses a loop, despite the slightly different keyword which creates it.
@David - "This still uses a loop" - yes, but so does the answer of Dennis_E. Only "hidden". So i don't quite get the downvotes either.
@Corak: Sure, anything is going to use a loop somewhere. There's no LINQ in the compiled IL, for example. I'm just guessing that "just use for instead of foreach" is probably missing the intent of the question and comes across as... unhelpful.
@David Yeah, I know, but people just don't understand LINQ does a foreach internally, and is less efficient than a simple for loop. OP's didn't want to use foreach, well then, this is the only answer that doesn't use it. Lazy question, lazy answer (but this actually answers the question).
@ShlomiBorovitz - "being it more memory-efficient isn't making it better." - well, that's the thing. More memory-efficient is better, when memory efficiency is the goal. OP just wants "better" without defining what he means by that. It seems like he isn't out for memory efficiency. But then what? Faster? The accepted answer won't be "faster" (it won't be much slower either). More maintainable? Probably. As you said, we don't know.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.