📖 Scenario: Imagine you have a list of words and you want to check which words are palindromes (words that read the same backward and forward). You want to add this check directly to the string type without changing the original string class.
🎯 Goal: You will create an extension method to check if a string is a palindrome. This shows why extension methods are useful: they let you add new features to existing types without changing their code.
📋 What You'll Learn
Create a static class called StringExtensions
Inside it, create a public static method called IsPalindrome that extends string
Use the this keyword in the method parameter to make it an extension method
Check if the string reads the same backward and forward
Use the extension method on a string variable to print if it is a palindrome
💡 Why This Matters
🌍 Real World
Extension methods are used in many C# libraries to add helpful features to existing types, like adding sorting or filtering methods to collections.
💼 Career
Knowing how to write and use extension methods is important for writing clean, reusable, and maintainable code in professional C# development.
Progress0 / 4 steps
1
Create a string variable
Create a string variable called word and set it to the value "level".
C Sharp (C#)
Hint
Use string word = "level"; to create the variable.
2
Create the extension method class
Create a static class called StringExtensions and inside it, create a public static method called IsPalindrome that extends string using the this keyword in the parameter.
C Sharp (C#)
Hint
Use public static bool IsPalindrome(this string s) inside a static class.
3
Use the extension method
Use the extension method IsPalindrome on the variable word and store the result in a bool variable called result.
C Sharp (C#)
Hint
Call word.IsPalindrome() and assign it to result.
4
Print the result
Print the value of result using Console.WriteLine.
C Sharp (C#)
Hint
Use Console.WriteLine(result); to show the output.
Practice
(1/5)
1. Why do we need extension methods in C#?
easy
A. To replace inheritance completely
B. To add new functionality to existing types without modifying their source code
C. To create new classes from scratch
D. To make variables mutable
Solution
Step 1: Understand the purpose of extension methods
Extension methods allow adding new methods to existing types without changing their original code or creating a new derived class.
Step 2: Compare with other options
Options B, C, and D do not describe the purpose of extension methods correctly. They do not replace inheritance or create new classes, nor do they affect variable mutability.
Final Answer:
To add new functionality to existing types without modifying their source code -> Option B
Quick Check:
Extension methods add new actions without changing original types [OK]
Hint: Extension methods add features without changing original code [OK]
Common Mistakes:
Thinking extension methods replace inheritance
Confusing extension methods with new class creation
2. Which of the following is the correct syntax to declare an extension method in C#?
easy
A. public static void MyMethod(this int number) { }
B. public void MyMethod(int this number) { }
C. static public void MyMethod(int number) { }
D. public void this MyMethod(int number) { }
Solution
Step 1: Identify extension method syntax
An extension method must be static, inside a static class, and the first parameter must have the this keyword before the type it extends.
Step 2: Check each option
public static void MyMethod(this int number) { } correctly uses public static void MyMethod(this int number). Options A, B, and C have incorrect placement or missing this keyword.
Final Answer:
public static void MyMethod(this int number) { } -> Option A
Quick Check:
Extension method syntax requires static + this on first parameter [OK]
Hint: Look for 'static' and 'this' on first parameter [OK]
Common Mistakes:
Omitting 'static' keyword
Placing 'this' incorrectly
Declaring method as instance method
3. What will be the output of the following code?
public static class Extensions {
public static int Square(this int x) {
return x * x;
}
}
int number = 4;
Console.WriteLine(number.Square());
medium
A. Compilation error
B. 4
C. Runtime error
D. 16
Solution
Step 1: Understand the extension method behavior
The method Square extends int and returns the number multiplied by itself.
Step 2: Calculate the output for number = 4
Calling 4.Square() returns 4 * 4 = 16, which is printed.
Final Answer:
16 -> Option D
Quick Check:
4 squared = 16 [OK]
Hint: Extension method acts like instance method on type [OK]
Common Mistakes:
Expecting original number instead of squared
Confusing extension method call syntax
Thinking code causes errors
4. Identify the error in this extension method declaration:
public static class Extensions {
public int Double(this int x) {
return x * 2;
}
}
medium
A. Extension method cannot extend int type
B. Extension method must be inside a non-static class
C. Extension method must be static
D. Method cannot have parameters
Solution
Step 1: Check method declaration rules
Extension methods must be declared as static methods inside static classes.
Step 2: Identify the error in code
The method Double is not static, which violates extension method rules.
Final Answer:
Extension method must be static -> Option C
Quick Check:
Extension methods require static keyword on method [OK]
Hint: Extension methods must be static methods [OK]
5. You want to add a method IsPalindrome() to the string type without modifying the original string class. Which approach best uses extension methods to achieve this?
hard
A. Create a static class with a static method IsPalindrome(this string s) that checks palindrome logic
B. Inherit from string and add IsPalindrome() method
C. Modify the string class source code to add IsPalindrome()
D. Create a new class with a method IsPalindrome(string s) and call it manually
Solution
Step 1: Understand extension method usage
Extension methods allow adding new methods to existing types without modifying or inheriting from them.
Step 2: Evaluate options
Create a static class with a static method IsPalindrome(this string s) that checks palindrome logic correctly uses a static class and static method with this string s parameter to extend string. Options A and C are not possible because string is sealed and source code cannot be changed. Create a new class with a method IsPalindrome(string s) and call it manually requires manual calls and is not an extension method.
Final Answer:
Create a static class with a static method IsPalindrome(this string s) that checks palindrome logic -> Option A
Quick Check:
Extension methods add methods without inheritance or source changes [OK]
Hint: Use static class and static method with 'this' parameter [OK]
Common Mistakes:
Trying to inherit from sealed string class
Attempting to modify string source code
Not using 'this' keyword in method parameter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.