📖 Scenario: Imagine you are organizing a small library. You need to keep track of books and perform tasks like adding new books and showing all books. Doing these tasks repeatedly without methods can be confusing and messy.
🎯 Goal: You will create a simple program that uses methods to add books and display them. This will show why methods help keep code clean and easy to use.
📋 What You'll Learn
Create a list to store book titles
Create a method called AddBook to add a book title to the list
Create a method called ShowBooks to print all book titles
Use these methods to add and show books
💡 Why This Matters
🌍 Real World
In real software, methods help keep code clean and organized, making it easier to build and fix programs.
💼 Career
Understanding methods is essential for any programming job because they are the building blocks of writing clear and reusable code.
Progress0 / 4 steps
1
Create a list to store book titles
Create a list called books that can hold strings representing book titles.
C Sharp (C#)
Hint
Use List<string> to create a list that holds text values.
2
Create a method to add a book
Create a method called AddBook that takes a string parameter title and adds it to the books list.
C Sharp (C#)
Hint
Use the Add method of the list to add the new book title.
3
Create a method to show all books
Create a method called ShowBooks that prints each book title from the books list using a foreach loop.
C Sharp (C#)
Hint
Use a foreach loop to go through each book and print it.
4
Use methods to add and show books
In the Main method, call AddBook three times with these exact titles: "C# Basics", "Learn Programming", "Data Structures". Then call ShowBooks to print all book titles.
C Sharp (C#)
Hint
Call AddBook three times with the exact titles, then call ShowBooks.
Practice
(1/5)
1. Why do programmers use methods in C# programs?
easy
A. To organize code into small, clear parts for better understanding
B. To make the program run faster by skipping code
C. To write code only once and never use it again
D. To make the program use more memory
Solution
Step 1: Understand the purpose of methods
Methods help break down code into smaller, manageable pieces.
Step 2: Identify the correct reason
Organizing code into small parts makes it easier to read and maintain.
Final Answer:
To organize code into small, clear parts for better understanding -> Option A
Quick Check:
Methods organize code = A [OK]
Hint: Methods break code into parts for clarity [OK]
Common Mistakes:
Thinking methods speed up the program directly
Believing methods increase memory use
Assuming methods are for one-time code only
2. Which of the following is the correct way to define a method named Greet that returns nothing and takes no parameters in C#?
easy
A. def Greet() { Console.WriteLine("Hello"); }
B. void Greet() { Console.WriteLine("Hello"); }
C. function Greet() { Console.WriteLine("Hello"); }
D. Greet() => Console.WriteLine("Hello");
Solution
Step 1: Recall C# method syntax
In C#, methods use the return type, name, parentheses, and braces.
Step 2: Match syntax with options
void Greet() { Console.WriteLine("Hello"); } uses void return type and correct braces, matching C# syntax.
Final Answer:
void Greet() { Console.WriteLine("Hello"); } -> Option B
Quick Check:
C# method syntax = C [OK]
Hint: C# methods start with return type and name [OK]
The method PrintMessage prints "Hello World" when called.
Step 2: Check the method call
The method is called correctly with PrintMessage();, so it runs and prints the message.
Final Answer:
Hello World -> Option D
Quick Check:
Method call prints message = D [OK]
Hint: Method call runs code inside method [OK]
Common Mistakes:
Thinking method name prints instead of output
Assuming no output without return
Confusing syntax error with correct code
4. Identify the error in this C# method and how to fix it:
void ShowNumber()
{
Console.WriteLine(number);
}
int number = 5;
ShowNumber();
medium
A. The variable 'number' is used before declaration; move declaration above method
B. Method must return int instead of void
C. Missing semicolon after method call
D. Method name should be lowercase
Solution
Step 1: Check variable scope
The variable 'number' is declared after the method, so it's not visible inside the method.
Step 2: Fix variable scope
Move the declaration of 'number' before the method or pass it as a parameter.
Final Answer:
The variable 'number' is used before declaration; move declaration above method -> Option A
Quick Check:
Variable scope error = B [OK]
Hint: Variables must be declared before use in methods [OK]
Common Mistakes:
Thinking method return type causes error
Missing semicolon is not the issue here
Ignoring case sensitivity of method names
5. You want to reuse code that calculates the area of a rectangle multiple times with different sizes. How should you use methods in C# to do this efficiently?
hard
A. Write a method that prints the area directly without parameters
B. Copy and paste the area calculation code everywhere you need it
C. Write a method that takes width and height as parameters and returns the area
D. Use a global variable to store area and update it each time
Solution
Step 1: Identify the need for reusable code
Calculating area multiple times means code should be reusable with different inputs.
Step 2: Use method with parameters and return value
A method that takes width and height and returns area allows reuse without copying code.
Final Answer:
Write a method that takes width and height as parameters and returns the area -> Option C
Quick Check:
Reusable method with parameters = A [OK]
Hint: Use parameters and return to reuse code easily [OK]
Common Mistakes:
Copy-pasting code instead of using methods
Printing inside method limits reuse
Using global variables causes confusion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.