-1

So I was wondering if there's a way to add a string to a null or empty index in array list. So the thing that I want is that if the array list index is null or empty then I want it to be replaced by another text.

string[] namelist = new string[4] { "dodo", "klee", "diluc", null };
for (int i = 0; i < namelist.Count(); i++)
{
    if (namelist[i] == null)
    {
        *Replace Null Text*
    }
    else
        Console.WriteLine("Taken");
}

Thanks.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • 3
    `namelist[i] = "Whatever text you like"` – Peter Csala Aug 05 '21 at 10:41
  • 2
    Also, use `namelist.Length` rather than `namelist.Count()` -- it's slightly cheaper – canton7 Aug 05 '21 at 10:42
  • 1
    Does this answer your question? [How do I replace an item in a string array?](https://stackoverflow.com/questions/2349339/how-do-i-replace-an-item-in-a-string-array) –  Aug 05 '21 at 10:47
  • 1
    This question is basically: How to update arrays, which is a basic topic covered from all tutorials. – Tim Schmelter Aug 05 '21 at 10:50
  • 1
    @canton7: it really does't matter whether you use `Length` or `Count()`. This method is called only once and it checks if the source implements `ICollection` which is true for arrays, so it's using the arrays `Count` **property** (yes, arrays have it, it's simply using `Length`) instead of enumerating it. – Tim Schmelter Aug 05 '21 at 10:59
  • @TimSchmelter [The JIT disagrees](https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKGIAYACY8gOgBkBLAOwEcBuGjWIBmJqQYBhBgG8aDeU1HEUDALIAKbhgDaAXQYAzCBACUchbOoLrh6A01cMDDgwC8DOn2cMAPLYjsMFwA5hgAFl4cANRRZlY28pYJycwAnOpGENocuiZe8uYJAL6FDCXxBRWKTCoAcg46+plx1knJRlD2Wt7unt5+mSwSEACujup5zjEtyW3J1mkZxtm5+Qyl1uWbNEVAA==). It means an extra method call on every iteration, and means it can't elide the bounds-checks when accessing `namelist` elements. Agreed `Enumerable.Count()` optimizes to using `ICollection.Count` here, but that's not my point – canton7 Aug 05 '21 at 11:05
  • @canton7: Yes, i wanted to edit my comment(too late). You're right, in a for-loop the condition is checked on every iteration. However, i still would say this is micro optimization and you probably will never notice a difference between using Length or Count(). People will misunderstand your comment since they think that `Count()` will enumerate the sequence to count the array size which isn't the case. – Tim Schmelter Aug 05 '21 at 11:08
  • @TimSchmelter It's a bit of best practice though: you save a character, get something that's cheaper, and don't raise alarm bells in the heads of any reviewers which know that `Count()` *may* have to iterate the underlying collection. I didn't say nor imply that `Count()` would have to iterate the collection here -- I just said that using `Length` is slightly cheaper, which it is – canton7 Aug 05 '21 at 11:09

2 Answers2

1

You're using an array and so Count() doesn't apply, but it would for a collection type like List.

    string[] namelist = new string[4] { "dodo", "klee", "diluc", null };
    for (int i = 0; i < namelist.Length; i++)
    {
        if (string.IsNullOrEmpty(namelist[i]))
        {
            namelist[i] = "filled";
        }
        else
            Console.WriteLine("Taken");
    }
ChrisBD
  • 9,104
  • 3
  • 22
  • 35
  • 2
    `Enumerable.Count()` works on arrays, as well as any other `IEnumerable`. It's slightly less efficient here, but still works. – canton7 Aug 05 '21 at 10:58
-1

It's sometimes a good idea not to modify the array, but instead create a new one.

Here's the easy way:

namelist = namelist.Select(x => x ?? "*Replace Null Text*").ToArray();
Enigmativity
  • 113,464
  • 11
  • 89
  • 172