📖 Scenario: You have a list of daily tasks to complete. Sometimes, you finish the first task and want to remove it from the list to focus on the next ones.
🎯 Goal: Build a simple program that stores tasks in an array, removes the first task, and shows the updated list.
📋 What You'll Learn
Create an array called tasks with exactly these strings: "Wake up", "Brush teeth", "Eat breakfast", "Go to work"
Create an integer variable called length to store the number of tasks
Write code to remove the first task by shifting all other tasks one position to the left
Print the updated tasks array elements separated by -> and ending with null
💡 Why This Matters
🌍 Real World
Managing daily tasks or to-do lists often requires removing completed tasks from the beginning of the list.
💼 Career
Understanding array manipulation is fundamental for software development, especially when working with fixed-size data collections.
Progress0 / 4 steps
1
Create the tasks array
Create a string array called tasks with these exact values: "Wake up", "Brush teeth", "Eat breakfast", "Go to work"
DSA C#
Hint
Use string[] tasks = { ... }; to create the array with the exact tasks.
2
Create the length variable
Create an integer variable called length and set it to the length of the tasks array
DSA C#
Hint
Use int length = tasks.Length; to get the number of tasks.
3
Remove the first task by shifting
Use a for loop with variable i from 0 to length - 2 to shift each task one position to the left in the tasks array
DSA C#
Hint
Use a for loop to copy each element from the right to the left, starting at index 0.
4
Print the updated tasks
Use a for loop with variable i from 0 to length - 2 to print each task followed by " -> ", then print "null" at the end
DSA C#
Hint
Print each task except the last one, followed by " -> ", then print "null" on the same line.
Practice
(1/5)
1. What happens to the elements of an array when you delete the first element (at the beginning) in C#?
easy
A. All elements shift one position to the left, and the last element becomes unused.
B. The array size decreases automatically by one.
C. The first element is replaced by null but others stay the same.
D. The array elements remain unchanged.
Solution
Step 1: Understand array fixed size in C#
Arrays have fixed size; deleting an element does not reduce size automatically.
Step 2: Effect of deleting first element
To delete the first element, all other elements shift left by one to fill the gap.
Final Answer:
All elements shift one position to the left, and the last element becomes unused. -> Option A
Quick Check:
Deleting first element shifts left [OK]
Hint: Deleting at start shifts all elements left by one [OK]
Common Mistakes:
Thinking array size changes automatically
Assuming elements stay in place
Believing first element becomes null only
2. Which of the following is the correct syntax to delete the first element from an array named arr in C# by shifting elements?
easy
A. arr.RemoveAt(0);
B. for(int i = 0; i < arr.Length - 1; i++) arr[i] = arr[i + 1];
C. for(int i = 1; i < arr.Length; i++) arr[i] = arr[i - 1];
D. arr.Delete(0);
Solution
Step 1: Understand array element shifting
To delete first element, shift elements left starting from index 0 to length-2.
Step 2: Check syntax correctness
for(int i = 0; i < arr.Length - 1; i++) arr[i] = arr[i + 1]; correctly loops from 0 to length-2 and assigns arr[i] = arr[i+1].
Final Answer:
for(int i = 0; i < arr.Length - 1; i++) arr[i] = arr[i + 1]; -> Option B
Quick Check:
Loop from 0 to length-2 shifting elements left [OK]
Hint: Shift elements from index 0 to length-2 left [OK]
C. Loop direction is wrong; should shift elements left, not right.
D. The loop should start from 0 and go to arr.Length - 2.
Solution
Step 1: Analyze loop direction
The loop copies arr[i-1] to arr[i], shifting elements right, which is incorrect for deletion.
Step 2: Correct loop to shift elements left
Loop should assign arr[i] = arr[i+1] starting from i=0 to length-2.
Final Answer:
Loop direction is wrong; should shift elements left, not right. -> Option C
Quick Check:
Shift left to delete first element [OK]
Hint: Shift elements left, not right, when deleting at start [OK]
Common Mistakes:
Shifting elements in wrong direction
Trying to reduce array size directly
Ignoring last element cleanup
5. You have an array int[] arr = {3, 6, 9, 12, 15}. Write a method to delete the first element and return a new array with the remaining elements only (size reduced by one). Which approach is correct?
hard
A. Use arr.RemoveAt(0) to delete the first element.
B. Shift elements left in original array and keep the same size.
C. Set arr[0] = 0 and return the original array.
D. Create a new array of size arr.Length - 1 and copy elements from index 1 onward.
Solution
Step 1: Understand fixed size arrays
Arrays in C# have fixed size; to reduce size, create a new array.
Step 2: Copy elements except first
Create new array of size one less and copy elements from index 1 onward.
Final Answer:
Create a new array of size arr.Length - 1 and copy elements from index 1 onward. -> Option D
Quick Check:
New smaller array with elements from index 1 [OK]
Hint: Create new smaller array and copy from index 1 [OK]
Common Mistakes:
Trying to resize original array directly
Only shifting elements without resizing
Using non-existent RemoveAt method on array
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.