Studying extension methods using the book as an example of expanding the functional structure of an Int32
(int
keyword) in C# (we cannot inherit structures and change the source code in the case of Int32
), I realized that extension methods are needed for three things:
- If inheritance is not possible, as in the case with sealed classes;
- In case of the inability to change the source code;
- If it is possible, but undesirable, to change the source code of a class or structure.
I wonder what would be a typical example for the third case.
The question:
When I describe the class myself and I can make changes to it, without using the extension method, but why in this case I might need to use the extension method if I can just change the code in my class?
That is, I ask you to give an example when I can change the code, but this is undesirable and desirable use extension method.