An extension method is a special kind of static method that allows you to add new methods to existing types without modifying their source code or creating a new derived type.
Click to reveal answer
beginner
How do extension methods enable LINQ to work on collections?
LINQ uses extension methods to add query methods like Where and Select to any collection that implements IEnumerable<T>, allowing queries to be written directly on those collections.
Click to reveal answer
intermediate
Why can't LINQ methods be instance methods on collections?
Because collections like arrays or lists are defined in .NET libraries, we cannot change their code to add new instance methods. Extension methods let LINQ add query capabilities without modifying these types.
Click to reveal answer
beginner
What is the benefit of LINQ using extension methods for queries?
It allows LINQ to provide a consistent, readable query syntax on many different data sources without changing their original classes, making code easier to write and understand.
Click to reveal answer
intermediate
How does the compiler treat extension methods when you call them?
The compiler translates calls to extension methods as if they were static method calls, passing the instance as the first argument, enabling seamless syntax that looks like instance method calls.
Click to reveal answer
Why does LINQ use extension methods?
ATo add query methods to existing types without modifying them
BTo create new collection types
CTo replace instance methods in collections
DTo improve runtime performance
✗ Incorrect
LINQ uses extension methods to add query capabilities to existing types like IEnumerable without changing their original code.
Which interface must a collection implement to use LINQ extension methods?
AIDisposable
BIComparable
CICloneable
DIEnumerable<T>
✗ Incorrect
LINQ extension methods work on collections that implement IEnumerable.
What does the compiler do when you call an extension method?
ACalls an instance method directly
BIgnores the method
CCalls a static method passing the instance as the first parameter
DThrows a runtime error
✗ Incorrect
The compiler converts extension method calls into static method calls with the instance as the first argument.
Can you add instance methods directly to existing .NET collection classes?
AYes, by editing their source code
BNo, because they are sealed or external
CYes, using inheritance only
DNo, because C# does not support methods
✗ Incorrect
Existing .NET collection classes cannot be changed directly, so extension methods are used instead.
What is a key advantage of LINQ using extension methods?
AIt allows queries on many data types with the same syntax
BIt requires rewriting collection classes
CIt slows down code execution
DIt hides data from the programmer
✗ Incorrect
Extension methods let LINQ provide a uniform query syntax across different data sources.
Explain why LINQ depends on extension methods to add query capabilities to collections.
Think about how you can add new features to something you cannot change.
You got /4 concepts.
Describe how the compiler handles calls to LINQ extension methods behind the scenes.
Focus on what happens when you write something like collection.Where(...)
You got /4 concepts.
Practice
(1/5)
1. Why does LINQ in C# rely on extension methods?
easy
A. To add query capabilities to existing types without modifying them
B. To create new classes for each query operation
C. To replace all existing methods in a class
D. To make the code run faster by compiling queries separately
Solution
Step 1: Understand LINQ's design goal
LINQ aims to add query features to existing collections without changing their original code.
Step 2: Role of extension methods
Extension methods allow adding new methods to existing types externally, enabling LINQ queries without modifying those types.
Final Answer:
To add query capabilities to existing types without modifying them -> Option A
Quick Check:
LINQ uses extension methods to extend types [OK]
Hint: Extension methods add features without changing original classes [OK]