Strings
Working with text
Introduction
In this lesson, you'll learn about strings in C#. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C, you're familiar with working with text.
C# has its own approach to working with text, which we'll explore step by step.
The C# Way
Let's see how C# handles this concept. Here's a typical example:
string name = "Alice";
int len = name.Length;
// Concatenation
string greeting = "Hello, " + name + "!";
// or: string.Format("Hello, {0}!", name)
// Comparison
if (name == "Alice") { /* equal — == works on strings */ }
if (name.Equals("alice", StringComparison.OrdinalIgnoreCase)) { }
// Substring
string sub = name.Substring(1, 3); // "lic"
// or: name[1..4]
// Built-in methods
string upper = name.ToUpper(); // "ALICE"
string lower = name.ToLower();
string trimmed = " hi ".Trim();
string[] parts = "a,b,c".Split(',');
string joined = string.Join(",", parts);
bool starts = name.StartsWith("Al");
int idx = name.IndexOf("lic"); // 1Comparing to C
Here's how you might have written similar code in C:
#include <stdio.h>
#include <string.h>
char name[] = "Alice";
int len = strlen(name);
/* Concatenation needs buffer */
char buf[100];
snprintf(buf, sizeof(buf), "Hello, %s!", name);
/* Comparison */
if (strcmp(name, "Alice") == 0) { /* equal */ }
/* Substring */
char sub[4];
strncpy(sub, name + 1, 3);
sub[3] = '\0'; /* null terminate! */
/* Upper case — manual */
for (int i = 0; name[i]; i++)
name[i] = toupper((unsigned char)name[i]);You may be used to different syntax or behavior.
C# string is an immutable object; C is a null-terminated char array
You may be used to different syntax or behavior.
C# == compares string content (value equality); C == compares pointers
You may be used to different syntax or behavior.
No buffer overflow possible with C# strings
You may be used to different syntax or behavior.
C# string has 50+ built-in methods; C uses minimal string.h
Step-by-Step Breakdown
1. Strings Are Objects
C# strings are immutable objects. Every operation that 'modifies' a string creates a new one — no buffer management needed.
char buf[100]; snprintf(buf, 100, "Hello, %s", name);string greeting = "Hello, " + name;2. Equality with ==
C# overloads == for strings to compare content. C's == compares pointer addresses — a notorious source of bugs.
if (strcmp(s1, s2) == 0) { /* equal */ }if (s1 == s2) { /* content equal */ }3. StringBuilder for Performance
When building strings in a loop, use StringBuilder to avoid creating many intermediate objects — like C's buffer with snprintf.
var sb = new System.Text.StringBuilder();
for (int i = 0; i < 100; i++) sb.Append(i).Append(',');
string result = sb.ToString();Common Mistakes
When coming from C, developers often make these mistakes:
- C# string is an immutable object; C is a null-terminated char array
- C# == compares string content (value equality); C == compares pointers
- No buffer overflow possible with C# strings
Key Takeaways
- C# string is immutable object; no null terminator needed
- == compares content (not pointers) in C#
- Rich built-in methods replace string.h functions
- StringBuilder for loop concatenation performance