Create An X Delimited String From A Char List Using Linq Aggregate

A quick example of how to use the Aggregate method to create a string of delimited members, or in this case characters. You might wonder why this example, or at least you should. It’s true, the character list to delimited string is pretty useless, but some idiot from where I work needed it.

[TestMethod]
public void GetStringFromCharacters()
{
    var charList = Enumerable.Range(0, 10).Select(x => 'a').ToList();

    charList
        .Aggregate("", (inner, outer) => inner + outer.ToString() + ",")
        .Should()
        .Be("a,a,a,a,a,a,a,a,a,a");
}

The big thing here is the “” in the Aggregate method signature. That basically says that Inner is a string. If I didn’t have that specified, both inner and outer would be a character.

YAY

One thought on “Create An X Delimited String From A Char List Using Linq Aggregate”

Comments are closed.