📖 Scenario: Imagine you are working on a simple text editor that needs to handle and display user names correctly. Understanding how strings work and how they are stored in memory is important to manage text efficiently.
🎯 Goal: You will create a string variable, check its length, access characters by index, and print the string and its details to understand how strings are represented in memory.
📋 What You'll Learn
Create a string variable with a specific value
Create an integer variable to store the string length
Access a character at a specific index in the string
Print the string, its length, and the accessed character
💡 Why This Matters
🌍 Real World
Handling user names and text input correctly is important in many applications like chat apps, forms, and games.
💼 Career
Understanding string basics and memory representation helps in debugging, optimizing text processing, and working with APIs that require string manipulation.
Progress0 / 4 steps
1
Create a string variable
Create a string variable called userName and set it to the exact value "Alice".
DSA C#
Hint
Use the string keyword to declare a variable and assign the value with double quotes.
2
Get the length of the string
Create an integer variable called length and set it to the length of the string userName using the Length property.
DSA C#
Hint
Use userName.Length to get the number of characters in the string.
3
Access a character by index
Create a char variable called firstChar and set it to the first character of userName using index 0.
DSA C#
Hint
Use square brackets [] with index 0 to get the first character.
4
Print the string, length, and first character
Print the values of userName, length, and firstChar each on a new line using Console.WriteLine.
DSA C#
Hint
Use Console.WriteLine three times to print each value on its own line.
Practice
(1/5)
1. What does the string type in C# represent in memory?
easy
A. A list of integers representing ASCII codes
B. A sequence of characters stored as Unicode values
C. A mutable array of bytes
D. A pointer to a memory address without data
Solution
Step 1: Understand string storage in C#
C# strings store characters as Unicode, allowing international text.
Step 2: Compare options with string behavior
Only A sequence of characters stored as Unicode values correctly describes strings as sequences of Unicode characters.
Final Answer:
A sequence of characters stored as Unicode values -> Option B
Quick Check:
string = Unicode characters [OK]
Hint: Remember C# strings are Unicode sequences, not byte arrays [OK]
Common Mistakes:
Thinking strings are mutable arrays
Confusing ASCII with Unicode
Assuming strings are just pointers
2. Which of the following is the correct way to declare and initialize a string in C#?
easy
A. string s = "hello";
B. string s = hello;
C. string s = 'hello';
D. string s = "hello";
char[] arr = s;
Solution
Step 1: Check string literal syntax
In C#, string literals use double quotes, so "hello" is correct.
Step 2: Validate each option
string s = 'hello'; uses single quotes (for chars), B misses quotes, D tries to assign string to char array directly (invalid).
Final Answer:
string s = "hello"; -> Option A
Quick Check:
Strings use double quotes in C# [OK]
Hint: Use double quotes for strings, single quotes for chars [OK]
Common Mistakes:
Using single quotes for strings
Forgetting quotes around text
Assigning string directly to char array
3. What will be the output of the following C# code?