📖 Scenario: You are working on a simple text editor program. You need to understand how strings work in C# and why they cannot be changed once created.
🎯 Goal: Learn how to create strings, assign new values, and see how string immutability affects string operations.
📋 What You'll Learn
Create a string variable with a specific value
Create a second string variable to hold a new value
Concatenate two strings into a new string variable
Print the original and new string values to observe immutability
💡 Why This Matters
🌍 Real World
Understanding string immutability helps when working with text data in applications like text editors, messaging apps, or data processing.
💼 Career
Many programming jobs require handling text safely and efficiently. Knowing string immutability prevents bugs and improves performance.
Progress0 / 4 steps
1
Create a string variable
Create a string variable called originalText and set it to the value "Hello".
C Sharp (C#)
Hint
Use the syntax: string variableName = "value";
2
Assign a new string value
Create a string variable called newText and set it to the value "World".
C Sharp (C#)
Hint
Remember to use string keyword and assign the value with quotes.
3
Concatenate strings into a new variable
Create a string variable called combinedText that concatenates originalText and newText with a space between them.
C Sharp (C#)
Hint
Use the + operator to join strings and include a space as " ".
4
Print original and combined strings
Print the values of originalText and combinedText using Console.WriteLine to show that originalText did not change.
C Sharp (C#)
Hint
Use Console.WriteLine(variableName); to print each string on its own line.
Practice
(1/5)
1. What does it mean that strings in C# are immutable?
easy
A. Once created, the string cannot be changed; any modification creates a new string.
B. Strings can be changed directly without creating a new string.
C. Strings are stored as numbers internally.
D. Strings can only contain letters and no numbers.
Solution
Step 1: Understand immutability concept
Immutability means the value cannot be changed after creation.
Step 2: Apply to strings in C#
In C#, strings cannot be changed once created; any change makes a new string.
Final Answer:
Once created, the string cannot be changed; any modification creates a new string. -> Option A
Quick Check:
String immutability = cannot change original string [OK]
Hint: Remember: strings never change, they create new copies [OK]
Common Mistakes:
Thinking strings can be changed in place
Confusing immutability with read-only variables
Assuming strings only hold letters
2. Which of the following is the correct way to declare a string variable in C#?
easy
A. string name = "Alice";
B. String name = 'Alice';
C. string name = Alice;
D. String name = Alice;
Solution
Step 1: Check string declaration syntax
In C#, strings use double quotes and the lowercase keyword 'string'.
Step 2: Identify correct syntax
string name = "Alice"; uses lowercase 'string' and double quotes correctly.
Final Answer:
string name = "Alice"; -> Option A
Quick Check:
Use double quotes and string keyword [OK]
Hint: Use double quotes for strings and lowercase 'string' keyword [OK]
Common Mistakes:
Using single quotes for strings
Omitting quotes around text
Confusing 'string' with 'String' casing
3. What will be the output of this C# code?
string s = "hello";
s = s.ToUpper();
Console.WriteLine(s);
medium
A. hello
B. HELLO
C. Hello
D. Error
Solution
Step 1: Understand ToUpper() method
ToUpper() returns a new string with all letters uppercase.
Step 2: Assign new string to s and print
Variable s is updated to the uppercase string, so printing s shows "HELLO".
Final Answer:
HELLO -> Option B
Quick Check:
ToUpper() creates uppercase string [OK]
Hint: ToUpper() returns new uppercase string; assign it to variable [OK]
Common Mistakes:
Assuming original string changes without assignment
Expecting lowercase output
Thinking ToUpper() modifies string in place
4. Identify the error in this code snippet:
string s = "test";
s[0] = 'T';
Console.WriteLine(s);
medium
A. Strings cannot be indexed in C#.
B. Console.WriteLine requires parentheses.
C. Cannot assign to string index because strings are immutable.
D. Missing semicolon after string declaration.
Solution
Step 1: Check string indexing and assignment
Strings can be indexed to read characters but cannot assign to indexes.