0
0
C Sharp (C#)programming~5 mins

Why extension methods are needed in C Sharp (C#) - Quick Recap

Choose your learning style9 modes available
Recall & Review
beginner
What is the main purpose of extension methods in C#?
Extension methods allow 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 improve code readability?
They let you call new methods as if they were part of the original type, making the code cleaner and easier to understand.
Click to reveal answer
beginner
Why can't you always modify existing classes directly to add new methods?
Because the source code might be unavailable, or the class might be sealed or part of a library you don't control.
Click to reveal answer
beginner
What is a real-life analogy for extension methods?
It's like adding a new tool to your toolbox without changing the toolbox itself, so you can use the new tool whenever you want.
Click to reveal answer
intermediate
Can extension methods override existing methods of a class?
No, extension methods cannot override existing methods; they only add new methods that appear as if they belong to the type.
Click to reveal answer
Why are extension methods useful in C#?
AThey add new methods to existing types without changing their code
BThey allow changing private fields of a class
CThey replace existing methods in a class
DThey create new classes from scratch
Which of these is NOT a reason to use extension methods?
ATo add functionality to sealed classes
BTo add methods to types from external libraries
CTo modify the internal state of a class directly
DTo improve code readability
How do you call an extension method in C#?
AOnly through a static class instance
BAs if it were a method of the original type
CUsing reflection
DBy inheriting the original class
Can extension methods access private members of the extended class?
ANo, they can only access public members
BYes, always
COnly if marked internal
DOnly in the same assembly
Which keyword is used to define an extension method?
Aoverride
Bstatic
Cvirtual
Dthis
Explain why extension methods are needed and how they help when working with existing classes.
Think about situations where you can't change a class but want to add features.
You got /4 concepts.
    Describe a real-life example or analogy that helps understand the concept of extension methods.
    Imagine adding something new without breaking what you already have.
    You got /3 concepts.