C
C#

C to C#

10 lessons

Progress0%
1Variables & Types2Functions & Methods3Arrays & Lists4Structs → Classes5Memory Management6Strings7File I/O8Object-Oriented Programming9Generics and Collections10Async and Concurrency
All Mirror Courses
C
C#
Strings
MirrorLesson 6 of 10
Lesson 6

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.

Mirror Card
C
From C:

In C, you're familiar with working with text.

C#
In C#:

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:

C#
C# 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");      // 1

Comparing to C

Here's how you might have written similar code in C:

C
C (What you know)
#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]);
Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# string is an immutable object; C is a null-terminated char array

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# == compares string content (value equality); C == compares pointers

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

No buffer overflow possible with C# strings

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

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.

C
C
char buf[100]; snprintf(buf, 100, "Hello, %s", name);
C#
C#
string greeting = "Hello, " + name;

2. Equality with ==

C# overloads == for strings to compare content. C's == compares pointer addresses — a notorious source of bugs.

C
C
if (strcmp(s1, s2) == 0) { /* equal */ }
C#
C#
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.

C#
C#
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
Common Pitfall
Don't assume C# works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in C# to practice these concepts.
PreviousNext