Building Strings
Concatenation and StringBuilder.
Building Strings is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Joining Strings With +
The simplest way to build a string is to join pieces with the + operator.
This is called concatenation. It works well for a few pieces, like combining a greeting with a name.
class Program
{
static void Main()
{
string name = "Sam";
string greeting = "Hi, " + name;
System.Console.WriteLine(greeting);
}
}String Interpolation
Interpolation is a cleaner way to insert values into text.
Put a $ before the string and wrap variables in curly braces. The values are dropped into place automatically.
class Program
{
static void Main()
{
string name = "Sam";
int age = 9;
System.Console.WriteLine($"{name} is {age}");
}
}The Cost of += in Loops
Using += to grow a string inside a loop is slow.
Because strings are immutable, each += creates a brand new string and copies all the old characters. For many iterations this wastes time and memory.
string result = "";
for (int i = 0; i < 1000; i++)
result += i; // creates many stringsMeet StringBuilder
StringBuilder is built for assembling text efficiently.
It lives in the System.Text namespace. Instead of making a new string each time, it grows an internal buffer, which is much faster for many additions.
using System.Text;
StringBuilder sb = new StringBuilder();Appending With StringBuilder
Call Append to add text to a StringBuilder.
You can append many pieces one after another. The builder keeps everything in its buffer until you ask for the final string.
using System.Text;
class Program
{
static void Main()
{
StringBuilder sb = new StringBuilder();
sb.Append("Hello");
sb.Append(", world");
System.Console.WriteLine(sb.ToString());
}
}Getting the Final string
A StringBuilder is not itself a string.
When you are done appending, call ToString to get the finished text. After that you can print it or store it like any normal string.
using System.Text;
StringBuilder sb = new StringBuilder();
sb.Append("Done");
string final = sb.ToString();Appending in a Loop
This is where StringBuilder shines.
Append inside a loop and the builder grows efficiently, avoiding the copies that += would create. This program builds a string of numbers.
using System.Text;
class Program
{
static void Main()
{
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 5; i++)
sb.Append(i);
System.Console.WriteLine(sb.ToString());
}
}AppendLine
AppendLine works like Append but also adds a newline after the text.
It is handy for building multi-line output, such as a list where each item sits on its own line.
using System.Text;
class Program
{
static void Main()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Line 1");
sb.AppendLine("Line 2");
System.Console.Write(sb.ToString());
}
}Building From Characters
You can append single char values too, not just whole strings.
This is useful when transforming text character by character, such as uppercasing each letter as you build a new result.
using System.Text;
class Program
{
static void Main()
{
StringBuilder sb = new StringBuilder();
foreach (char c in "loud")
sb.Append(char.ToUpper(c));
System.Console.WriteLine(sb.ToString());
}
}When to Use Each Approach
For a few pieces, + or interpolation is clear and fine.
For building text in a loop or joining many pieces, prefer StringBuilder. The rule of thumb: a handful of joins use operators, many joins use a builder.
string.Join
When you already have a collection of pieces, string.Join combines them with a separator.
You pass the separator first, then the items. It is a concise alternative to looping for simple lists.
string[] parts = { "a", "b", "c" };
string joined = string.Join("-", parts);
// joined is "a-b-c"Quick Check
Test your understanding of building strings.
Recap
Use + or interpolation to join a few strings, and string.Join for a list of pieces.
For building text in loops, reach for StringBuilder: call Append or AppendLine, then ToString to get the result. It avoids the costly copies that += makes because strings are immutable.
Frequently asked questions
Is the “Building Strings” lesson free?
Yes — the full text of “Building Strings” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building Strings”?
Concatenation and StringBuilder. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building Strings” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The char Type
- Char Helper Methods
- Iterating Over Strings
- Building Strings