C#
IEnumerable
string conversion
programming
duplicate question

IEnumerablechar to string

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you have an IEnumerable<char> in C#, the standard way to turn it into a string is new string(chars.ToArray()). That answer is simple and correct because a string needs a contiguous immutable sequence of characters, while IEnumerable<char> may be lazy, streamed, or generated on demand.

The important thing is to distinguish between a sequence abstraction and a finished string value. Converting to a string forces full enumeration of the sequence, which is exactly what you want when the result really is text.

The Standard Conversion

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5IEnumerable<char> chars = "Hello, World!".Where(char.IsLetter);
6string result = new string(chars.ToArray());
7
8Console.WriteLine(result);

This works in two steps:

  • enumerate the characters
  • copy them into a char[]
  • build the immutable string

That is the normal and explicit approach.

Why ToArray() Is Usually Necessary

A string constructor accepts a char[], not an arbitrary IEnumerable<char>. That is because a string must know all of its characters in contiguous memory at creation time.

So even if the source sequence is lazy, converting it to a string means materializing it first.

That is not wasted work. It is the unavoidable step of turning an abstract sequence into a concrete text value.

If You Already Have a char[]

If the data is already in a char[], skip ToArray().

csharp
char[] chars = { 'H', 'e', 'l', 'l', 'o' };
string result = new string(chars);
Console.WriteLine(result);

That avoids an extra copy and is the most direct conversion path.

StringBuilder Is Better for Incremental Construction

If characters are being produced incrementally in a loop, StringBuilder can be a better fit than first building an IEnumerable<char> and then converting it.

csharp
1using System.Text;
2using System.Linq;
3
4IEnumerable<char> chars = "abc123xyz".Where(c => !char.IsDigit(c));
5
6var builder = new StringBuilder();
7foreach (char c in chars)
8{
9    builder.Append(c);
10}
11
12string result = builder.ToString();
13Console.WriteLine(result);

This is especially natural when your code is already appending over time rather than simply converting an existing sequence.

What Not to Do

Avoid repeated string concatenation in a loop.

csharp
1string result = "";
2foreach (char c in chars)
3{
4    result += c;
5}

That creates many intermediate strings because strings are immutable. It is the least efficient style here.

Also avoid treating IEnumerable<char> like IEnumerable<string>. If the source is already characters, keep it as characters until the final string constructor.

Lazy Sequences Still Execute on Conversion

One subtle point is that IEnumerable<char> may contain real logic, not just stored characters. Converting it to a string triggers enumeration, so the conversion may execute filtering, mapping, or even external work embedded in the iterator.

That means new string(chars.ToArray()) is not just a passive cast. It evaluates the sequence fully.

If the sequence is infinite or unexpectedly expensive, the conversion is the wrong operation.

A Small Helper Can Be Fine

If this pattern appears often, a helper can keep call sites readable.

csharp
1using System.Collections.Generic;
2using System.Linq;
3
4public static class TextHelpers
5{
6    public static string ToText(IEnumerable<char> chars)
7    {
8        return new string(chars.ToArray());
9    }
10}

The helper does not change the underlying approach. It just packages the standard conversion in one place.

Common Pitfalls

The biggest mistake is assuming an IEnumerable<char> can become a string without being fully enumerated. Another is using repeated += concatenation, which creates unnecessary temporary strings. Developers also sometimes forget that lazy iterators may perform work during enumeration, so conversion can trigger more than just a memory copy. Finally, if you already have a char[], calling ToArray() again only adds unnecessary overhead.

Summary

  • The standard conversion is new string(chars.ToArray()).
  • If you already have a char[], pass it directly to the string constructor.
  • Use StringBuilder when characters are being built incrementally.
  • Avoid repeated += concatenation in loops.
  • Remember that converting IEnumerable<char> to string forces full enumeration.

Course illustration
Course illustration

All Rights Reserved.