Why not to use extension methods?

Why not to use extension methods?

Times to not use extension methods:

  • when polymorphism is critical; you cannot guarantee that your code will be the version that gets executed with an extension method, as methods directly on the type take precedence.
  • when you need access to private/protected members.

What is method hiding in C#?

C# also provides a concept to hide the methods of the base class from derived class, this concept is known as Method Hiding. It is also known as Method Shadowing. In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword.

Why do we need method hiding?

It tells us to use the new keyword to hide the inherited member. So, by using the new modifier in the derived class method, it hides the implementation of the base class method. This is called Method Hiding. It allows you to provide a new implementation for a derived class.

Why are extension methods important in the BCL?

Actually most of the extension methods in the BCL apply not to classes, but to interfaces, which is a key power of extension methods. For example, almost all of LINQ to objects is implemented as extensions to the IEnumerable interface, and not to the classes List , HashSet , etc.

Why do we use extension methods in LINQ?

For example, almost all of LINQ to objects is implemented as extensions to the IEnumerable interface, and not to the classes List , HashSet , etc. Because interfaces do not have functionality (just a contract), adding an extension method to a interface gives you a way to an interface itself.

Why do we use extension methods in Microsoft?

Microsoft tends to use extension methods for functionality that will apply to a broad array of different code bases. For example, LINQ is implemented almost entirely as extension methods. (and most, if not all of the methods you point out are generic LINQ methods, not specific to those classes).

Why is the methodb extension method never called?

The MethodB extension method is never called because its name and signature exactly match methods already implemented by the classes. When the compiler cannot find an instance method with a matching signature, it will bind to a matching extension method if one exists.