I'm new to VB.Net, and I am curious about the use cases for extension methods. Specifically, why would I use an extension method when I have inheritance and interfaces at my disposal? At first glance, extension methods don't seem to me like a very OO practice, and it seems like they would lead to less readable code as opposed to using subclasses and/or interfaces to achieve the same purpose. Is there something special about extension methods that I'm missing? When do you use them?
When/Why would I use an extension method in VB.Net?
322 Views Asked by Sam Hazleton At
2
There are 2 best solutions below
0
Markus
On
You are right that extension methods are not a very important tool in the OO-toolbox. So you might not use them very often to extend classes in your own code.
As you also write, they might lead to confusion when reading other developers' code because you can only use extension methods that are located in a namespace that you have imported.
Nevertheless, extension methods are very handy if you want to create helper methods that extend classes that you cannot change. That is the main use case extension methods are created for.
Related Questions in VB.NET
- If...Then...Else Visual Basic 2012
- Detecting whether a mouse button is down in vb.net
- IOrderedEnumerable to vb.net IOrderedEnumerable Conversion
- vb.net Get PrivateMemorySize64 and process id sort by memory size
- Cannot insert values into database
- check validation of an expression with Regex
- VB.NET KeyNotFoundException from String()
- How do i display data that are in between 2 values in a DDL?
- VB.net: How to make original variable value fulfill 2 statements?
- Login form by using a new database, made in VB
- Get Text from listbox in VB.net
- error handing for uploading large size file
- XML Null Element Visual Basic
- Using chart and tooltip
- vb2010 Express - Operator '=' is not defined
Related Questions in OOP
- Access objects variable & method by name
- Why does compiler recognize while(true) at compile time but not if(true)
- Pass variables to extended class
- Cast a superclass type to a subclass type?
- Understanding difference in Swift properties for structs and classes in assignment
- Does exist any way to force child class to have at least one field with a specified attribute?
- Symfony : is it better to use a trait or an intermediary class to complete Controller one?
- (Java) What kind of argument is this? With a
- C++ Implementing a Queue of cars in OOP
- Inheritance in openERP (odoo)
- missing 1 required positional argument: 'key'
- how can Object class in ruby be an instance of it's subclass, class "Class"
- How to force others to obey a specific layout for a child class?
- Class variables in OOP
- define_method in a class method
Related Questions in INHERITANCE
- Inheritance in Java, apparent type vs actual type
- Why does compiler recognize while(true) at compile time but not if(true)
- How to write tests for classes with inheritance
- How to make methods from parent class use a different variable?
- Why put methods on the prototype of a class instead of declaring them in the constructor?
- AbstractMethodError when mixing in trait nested in object - only when compiled and imported
- Inheritance and Polymorphism in C#
- WCF reusing types with inheritance - cannot generate client code
- Inherit jQuery-like pattern
- C++ inheritance, interfaces
- Visual C++ - Virtual method is not overriden
- Inheritance with base class constructor with parameters
- Access Parent Class without declaring property
- Class factory to create derived classes c++
- Child element not to inherit parent property (not style) in html
Related Questions in INTERFACE
- C++ inheritance, interfaces
- Multiple service contract inter-commnication
- Passing Variables from VBA to R-Script
- Static interface equivalent C#
- What text (in English) should I use when asking the user to overwrite a document?
- Java generic class that contains an instance of implementation of generic interface
- HuggableInterface in PHP and PSR-8
- How do I define constants that must be overridden inside an interface?
- Symfony : is there a best practice about the directories' name including traits and interfaces?
- How to design structure with modifiable fields?
- Why introducing Autocloseable instead of extending the possible exceptions in Closeable
- CI accessing property on interface
- C# list of "Type" that implements specific interface
- ResultTransformer interface in java
- How can I guarantee that a class that implements an interface also extends a class?
Related Questions in EXTENSION-METHODS
- How to make extension method which should returns generic type but only nullable numeric datatype in vb.net
- How to extend string to deserialize binary data
- Adding new object property dynamically from extension method
- PHP Sibling Class Inheritance
- Creating a table method on a user defined type (like like 'nodes' on the XML data type)
- Why does this extension method not initialize the returned object?
- How do I make this Python extension work on windows (clear IDLE)?
- When/Why would I use an extension method in VB.Net?
- Extension methods, which is a better choice
- Extension Method for Interface in Internal Class
- Change state of List<T> inside extension method
- Inheritance and extension method
- Swift Dictionary access value using key within extension
- UIImage Extension Not Working Properly
- Swift3, extension to mutablizate a String array
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Before implementing an extension method, people should always try to see if it could fit inside a standard OO class.
However, there's 2 situations I can think of where those extension may be very handy:
1) Extension of primitive type: You don't want to derive a primitive type (like Int or Date) just to add an method to it. I'm not even sure if you're allowed to derive a primitive type but even if you could, it doesn't mean that you should.
2) Extension of common object: Let's say you want to write some addons functionalities to a very common object; let's say add a ToJSON method to a DbDataReader. You may force users the use your derived Type of DbDataReader (CustomDbDataReader) but they would have to change the code all over the place to use your new class. Using an extension would allow them to use your new method on a native DbDataReader.
You'll also notice that the DbDataReader is an abstract class. SqlDataReader and OleDbDataReader both derive from the DbDataReader class. If you want to write a legit OO function, you would have to write one for SqlDataReader and another one for the OleDbDataReader, even if the function does exactly the same thing.