Strings & Text: immutability, interpolation, verbatim, StringBuilder, comparison
Understand string immutability, use interpolation, escape and verbatim strings, build text efficiently with StringBuilder, compare safely, and review char/Unicode basics.
Strings & Text: immutability, interpolation, verbatim, StringBuilder, comparison is a free C# Academy lesson on CoddyKit — lesson 1 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Text overview
Goal: Use strings safely and efficiently.
- Immutability
- Interpolation & escapes
- Verbatim strings
- StringBuilder
- Comparison & culture notes
Immutability
Immutable: methods like ToUpper or Replace return new strings; originals stay the same.
using System;
public class Program
{
public static void Main(string[] args)
{
string s = "hello";
string upper = s.ToUpper(); // new string
Console.WriteLine("s = " + s); // hello
Console.WriteLine("upper = " + upper);// HELLO
string replaced = s.Replace("h", "y"); // new string
Console.WriteLine("replaced = " + replaced);
}
}
Interpolation & escapes
Interpolation uses $"...". Common escapes: \\n new line, \\" quote, \\\\ backslash.
using System;
public class Program
{
public static void Main(string[] args)
{
string name = "Ada";
int score = 95;
// C# 6 string interpolation and escapes
string msg = $"Hi {name}, score = {score}";
string quoted = "She said \"hi\"";
string line = "first line\nsecond line";
Console.WriteLine(msg);
Console.WriteLine(quoted);
Console.WriteLine(line);
}
}
Verbatim strings
Verbatim strings start with @: ideal for Windows paths and multi-line text.
using System;
public class Program
{
public static void Main(string[] args)
{
// Verbatim string keeps backslashes and newlines as typed
string path = @"C:\Projects\Demo\readme.txt";
string multi = @"Line1
Line2";
Console.WriteLine(path);
Console.WriteLine(multi);
}
}
StringBuilder
For many concatenations, use StringBuilder to reduce allocations and improve performance.
using System;
using System.Text;
public class Program
{
public static void Main(string[] args)
{
var sb = new StringBuilder();
for (int i = 1; i <= 3; i = i + 1)
{
sb.Append("Item ").Append(i).Append(", ");
}
string result = sb.ToString();
Console.WriteLine(result);
}
}
Compare & culture; char
Use StringComparison to control comparisons (e.g., OrdinalIgnoreCase). char is a UTF-16 code unit; some emojis need surrogate pairs (beyond this intro).
using System;
using System.Globalization;
public class Program
{
public static void Main(string[] args)
{
string a = "file";
string b = "FILE";
// Case-insensitive, culture-invariant comparison
bool same = string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Same (ord ignore case) = " + same);
// Culture example: ToUpper with current culture
string iDot = "i";
string upper = iDot.ToUpper(CultureInfo.CurrentCulture);
Console.WriteLine("Upper(i) in current culture = " + upper);
}
}
Immutability meaning
Recap
Recap: Strings are immutable; use interpolation and escapes; prefer @ for paths; for many appends use StringBuilder; compare with StringComparison; remember char is a UTF-16 unit.
Frequently asked questions
Is the “Strings & Text: immutability, interpolation, verbatim, StringBuilder, comparison” lesson free?
Yes — the full text of “Strings & Text: immutability, interpolation, verbatim, StringBuilder, comparison” is free to read here on the web, and the C# Academy course includes 3 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 “Strings & Text: immutability, interpolation, verbatim, StringBuilder, comparison”?
Understand string immutability, use interpolation, escape and verbatim strings, build text efficiently with StringBuilder, compare safely, and review char/Unicode basics. 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 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Strings & Text: immutability, interpolation, verbatim, StringBuilder, comparison” 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
- Strings & Text: immutability, interpolation, verbatim, StringBuilder, comparison
- StringBuilder, comparison & culture notes
- char, Unicode basics, simple parsing/formatting