What Advantages of Extension Methods have you found? [closed]
Asked Answered
D

30

85

A "non-believer" of C# was asking me what the purpose to extension methods was. I explained that you could then add new methods to objects that were already defined, especially when you don't own/control the source to the original object.

He brought up "Why not just add a method to your own class?" We've been going round and round (in a good way). My general response is that it is another tool in the toolbelt, and his response is it is a useless waste of a tool... but I thought I'd get a more "enlightened" answer.

What are some scenarios that you've used extension methods that you couldn't have (or shouldn't have) used a method added on to your own class?

Diegodiehard answered 28/1, 2009 at 14:56 Comment(2)
I think there are definitely valid points people can make (and have made) in support of extension methods... but there's definitely no scenario where one "couldn't have" used static methods in place of extension methods. Extension methods really are just static methods, accessed a different way. Just something to keep in mind.Grillparzer
@DanTao, on a lighter note, the one thing that makes calling by extension method irreplaceable are their very naming. Extensions.To(1, 10) is meaningless and 1.To(10) is descriptive. Of course I understand the technical side you're talking about, just saying. In fact there are scenarios where one "couldn't have" used extension approach in place of static methods, reflection for instance, another case is dynamic.Essence
R
35

I think extension methods help a lot when writing code, if you add extension methods to basic types you'll get them quicky in the intellisense.

I have a format provider to format a file size. To use it I need to write:

Console.WriteLine(String.Format(new FileSizeFormatProvider(), "{0:fs}", fileSize));

Creating an extension method I can write:

Console.WriteLine(fileSize.ToFileSize());

Cleaner and simpler.

Register answered 28/1, 2009 at 14:56 Comment(8)
a more descriptive name for your extension method would make it cleaner still. e.g. fileSize.ToFormattedString();Stormy
mmm, ToFormattedFizeSize() maybe, it's an extension method for a Long type, ToFormattedString() is not a descriptive nameRumrunner
point made, though. when i read fileSize.ToFileSize() i ask why the duplication? what's it actually doing? i know that it's only an example, but descriptive names help make it clean and simple.Stormy
you're right as with tests it's very important to choose the right nameRumrunner
That was the example I was going to post. My most used extension method is adding String.Format like syntax to Trace.WriteLine()Ulbricht
I'm glad we can all agree on the name to use and the semantics it should have, would hate to end up with 15 extension methods that do nearly the same thing.Meggie
This isn't really answering the question directly, though, since the example you provided doesn't need to be an extension method. You could have had LongToFileSize(fileSize), which is just as concise and arguably just as clear.Grillparzer
You're right Dan, the generated IL is the same, it's a tool, you can use it or not. I find it really useful, particulary extending sealed classes or just not inherit. Advantages, like others said, code readability (cleaner and simpler), tooling, fluent...Rumrunner
I
84

The only advantage of extension methods is code readability. That's it.

Extension methods allow you to do this:

foo.bar();

instead of this:

Util.bar(foo);

Now there are a lot of things in C# that are like this. In other words there are many features in C# that seem trivial and don't have great benefit in and of themselves. However once you begin combining these features together you begin to see something just a bit bigger than the sum of its parts. LINQ benefits greatly from extension methods as LINQ queries would be almost unreadable without them. LINQ would be possible without extension methods, but not practical.

Extension methods are a lot like C#'s partial classes. By themselves they are not very helpful and seem trivial. But when you start working with a class that needs generated code, partial classes start to make a lot more sense.

Ink answered 28/1, 2009 at 14:56 Comment(6)
This becomes even more important if you are using chained methods like x.Foo(something).Bar(somethingelse).Baz(yetmoresomething). This is most prevalant in the IEnumerable<> extension methods and significantly increase readabilityNkvd
The thing I don't get is that foo.bar() suggests to me that bar() is a method of foo. So when it doesn't work I end up looking in the wrong place. I'm not sure this is actually a readability improvement for me.Gneiss
Right clicking bar() in VS IDE and chosing to Goto the Definition would bring you to the right place.Liberec
The only advantage of 98% of language constructs are code readability. Anything past a branch operator, label, goto and increment is there to make your life a little easier.Stanger
That's not entirely true, there is some situations that extension methods are the only way to extend a class. Besides sealed classes I use them in a pretty unique situation. See my post below.Rutland
The only advantage of extension methods is code readability. That's it. = false. An extension method allows a simple data model to be extended with methods without cluttering the data model with function definitions.Spratt
R
35

I think extension methods help a lot when writing code, if you add extension methods to basic types you'll get them quicky in the intellisense.

I have a format provider to format a file size. To use it I need to write:

Console.WriteLine(String.Format(new FileSizeFormatProvider(), "{0:fs}", fileSize));

Creating an extension method I can write:

Console.WriteLine(fileSize.ToFileSize());

Cleaner and simpler.

Register answered 28/1, 2009 at 14:56 Comment(8)
a more descriptive name for your extension method would make it cleaner still. e.g. fileSize.ToFormattedString();Stormy
mmm, ToFormattedFizeSize() maybe, it's an extension method for a Long type, ToFormattedString() is not a descriptive nameRumrunner
point made, though. when i read fileSize.ToFileSize() i ask why the duplication? what's it actually doing? i know that it's only an example, but descriptive names help make it clean and simple.Stormy
you're right as with tests it's very important to choose the right nameRumrunner
That was the example I was going to post. My most used extension method is adding String.Format like syntax to Trace.WriteLine()Ulbricht
I'm glad we can all agree on the name to use and the semantics it should have, would hate to end up with 15 extension methods that do nearly the same thing.Meggie
This isn't really answering the question directly, though, since the example you provided doesn't need to be an extension method. You could have had LongToFileSize(fileSize), which is just as concise and arguably just as clear.Grillparzer
You're right Dan, the generated IL is the same, it's a tool, you can use it or not. I find it really useful, particulary extending sealed classes or just not inherit. Advantages, like others said, code readability (cleaner and simpler), tooling, fluent...Rumrunner
C
34

Don't forget tooling! When you add an extension method M on type Foo, you get 'M' in Foo's intellisense list (assuming the extension class is in-scope). This make 'M' much easier to find than MyClass.M(Foo,...).

At the end of the day, it's just syntactic sugar for elsewhere-static-methods, but like buying a house: 'location, location, location!' If it hangs on the type, people will find it!

Cough answered 28/1, 2009 at 14:56 Comment(2)
There is also a slight negative to this: extension methods (such as those from System.Linq fill up IntelliSense autocomplete with a longer list, making it a little harder to navigate.Hewes
@Jared: ... but only if you've imported the namespace where the extension methods reside, so you actually have a small degree of control over this "IntelliSense pollution". Second, let's not forget that there are classes in the BCL (such as String) that come with a fairly long list of instance methods, so this problem is not specific to extension methods.Carleycarli
S
29

Two more benefits of extension methods that i have come across:

  • A fluent interface can be encapsulated in a static class of extension methods, thereby achieving a separation of concerns between the core class and it's fluent extensions; I've seen that achieve greater maintainability.
  • Extension methods can be hung off of interfaces, thereby allowing you to specify a contract (via an interface) and an associated series of interface-based behaviors (via extension methods), again offering a separation of concerns. An example are the Linq extension methods like Select(...), Where(...), etc. Hung off the IEnumerable<T> interface.
Stormy answered 28/1, 2009 at 14:56 Comment(2)
Can you please quote some code examples for both the points to help them understand better? It appears to be an interesting view point.Ornithic
yeah an example for the 2nd point would be nice.Hulbert
V
27

Some of the best uses I had for extension methods is the ability to:

  1. Extend functionality on third party objects (whether commercial or internal to my company but managed by a separate group), which in many cases will be marked as sealed.
  2. Create default functionality for interfaces without having to implement an abstract class

Take for example, IEnumerable<T>. While it is rich in extension methods, I found it annoying that it did not implement a generic ForEach method. So, I made my own:

public void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
{
    foreach ( var o in enumerable )
    {
        action(o);
    }
}

Voila, all my IEnumerable<T> objects regardless of implementing type, and whether or not I wrote it or someone else did now have a ForEach method by adding an appropriate "using" statement in my code.

Vulcanite answered 28/1, 2009 at 14:56 Comment(3)
+1 I love this extension, I wrote one just like it. So very usefulOrangy
Note that the method needs to be static. I suggest reading blogs.msdn.com/ericlippert/archive/2009/05/18/… before deciding to use this extension.Horus
Reason why implementing ForEach extension method on IEnumerable isn't appreciated - blogs.msdn.microsoft.com/ericlippert/2009/05/18/…Ornithic
L
12

One of the great reasons for using extension methods is LINQ. Without extension methods a lot of what you can do in LINQ would be very hard. The Where(), Contains(), Select extension methods means a lot more functionality is added to existing types without changing their structure.

Lolita answered 28/1, 2009 at 14:56 Comment(3)
its not 'one of', its THE reason for extension methods.Blackamoor
How so? It is one of the many reasons for having extension methods, not the sole requirement. I can use extension methods without using LINQ.Lolita
I think he meant it was the reason they were created but yes, he makes it sound like they are the only proper use for them.Fishnet
N
9

There are plenty of answers about the advantages of extensions methods; how about one addressing the disadvantages?

The biggest disadvantage is that there's no compiler error or warning if you have a regular method and an extension method with the same signature in the same context.

Suppose you create an extension method applying to a particular class. Then later someone creates a method with an identical signature on that class itself.

Your code will compile, and you may not even get a runtime error. But you're no longer running the same code as before.

Naphthalene answered 28/1, 2009 at 14:56 Comment(3)
4. Think twice before extending types you don't own. If you only write extension methods for types you own, then you never have to worry about your extensions being broken by changes to the type being extended. On the other hand, if you are extending other peoples types than you are essentially at their mercy.Lubra
...Alternatively, you can minimize this risk by choosing names that are unlikely to be used by someone else, or by minimizing the total number of extension methods you define (i.e reducing surface area). One technique for doing this may be to write one extension method that converts the underlying object to a different type that is controlled by you.Lubra
Extension Methods (C# Programming Guide): In general, you will probably be calling extension methods far more often than implementing your own.Lubra
H
8

Fluent Interfaces and Context Sensitivity as demonstrated by Greg Young on CodeBetter

Hasbeen answered 28/1, 2009 at 14:56 Comment(0)
C
5

I would like to support the other answers here that mention improved code readability as an important reason behind extension methods. I'll demonstrate this with two aspects of this: method chaining vs. nested method calls, and cluttering of a LINQ query with meaningless static class names.


Let's take this LINQ query as an example:

numbers.Where(x => x > 0).Select(x => -x)

Both Where and Select are extension methods, defined in the static class Enumerable. Thus, if extension methods didn't exist, and these were normal static methods, the last line of code would essentially have to look like this:

Enumerable.Select(Enumerable.Where(numbers, x => x > 0), x => -x)

See how much nastier that query just got.


Second, if you now wanted to introduce your own query operator, you would naturally have no way of defining it inside the Enumerable static class, like all the other standard query operators, because Enumerable is in the framework and you have no control over that class. Therefore, you'd have to define your own static class containing extension methods. You might then get queries such as this one:

Enumerable.Select(MyEnumerableExtensions.RemoveNegativeNumbers(numbers), x => -x)
//                ^^^^^^^^^^^^^^^^^^^^^^
//                different class name that has zero informational value
//                and, as with 'Enumerable.xxxxxx', only obstructs the
//                query's actual meaning.
Carleycarli answered 28/1, 2009 at 14:56 Comment(0)
T
4

There are heaps of great answers above about what extension methods let you do.

My short answer is - they nearly eliminate the need for factories.

I'll just point out that they are not a new concept and one of the biggest validations of them is that they are a killer feature in Objective-C (categories). They add so much flexibility to framework-based development that NeXT had NSA and Wall Street financial modelers as major users.

REALbasic also implements them as extends methods and they have been of similar use there simplifying development.

Therontheropod answered 28/1, 2009 at 14:56 Comment(0)
T
4

My personal argument for Extension methods is, they fit very well into an OOP design: consider the simple method

bool empty = String.IsNullOrEmpty (myString)

in comparison to

bool empty = myString.IsNullOrEmpty ();
Trefor answered 28/1, 2009 at 14:56 Comment(10)
I don't see what you example has to do with OOP. What do you mean?Ink
it "seems" that the method belongs to the objectTrefor
I think the one MAIN point about extension methods is NEVER EVER write an extension method that has code for null instances of the type that you are extending the method to. Bill Wagner has a great portion of his book on this topic here: tinyurl.com/76x24dLolita
This is a bad example (can throw NullReferenceException) but he's on the right track. A better example might be replacing Math.abs(-4) with something like -4.abs();Kaifeng
to my knowledge (and to my experience, if memory serves), myString.IsNullOrEmpty() does not need to throw a NullReferenceException because extension methods do not require an object instance in order to fire.Stormy
As it may not be the optimal example, its 100% working, try testing: String x = null; Assert.AreEqual (true, x.IsNullOrEmpty ()); x = String.Empty; Assert.AreEqual (true, x.IsNullOrEmpty ());Trefor
Well color me surprised. I guess it is just syntactic sugar...Kaifeng
From an OOP perspective, it follows the Open/Close Principle. You can comfortably close your classes yet still extend it later, if necessary.Reborn
+1 I thought about trying this, assumed it wouldn't work. It works, and is incredibly useful.Orangy
I'm personally not a fan of extension methods that are intended to work with a null instance -- it looks like one thing to the reader, but then does something else.Haulm
T
3

I agree that extension methods increases readability of code, but it's really nothing else than static helper methods.

IMO using extension methods for adding behaviour to your classes can be:

Confusing: Programmers might believe that methods are a part of the extended type, thus not understanding why the methods are gone when the extension-namespace isn't imported.

An antipattern: You decide to add behaviour to types in your framework using extension methods, then shipping them off to some person which into unit testing. Now he's stuck with a framework containing a bunch of methods he can't fake.

Tinctorial answered 28/1, 2009 at 14:56 Comment(2)
It's not true that a developer is stuck with a bunch of methods he can't fake. These extension methods eventually call some method on the class/interface they're extending and this method can be intercepted given that it is virtual.Kinnon
Well, then it depends on what is done in the extension method. Let's say you use provided extension methods on an interface, not even knowing they are extension methods. Then you wan't to fake a method call to an interface, but you realise that you are actually invoking a static method. A static method call cannot be intercepted by a proxy based faking API, so your only option is to do reflection on the extension method to find out which methods to actually fake. Still antipattern when combined with unit testing IMO.Tinctorial
R
3

Extension methods can also help keep your classes and class dependencies clean. For instance, you may need a Bar() method for the Foo class everywhere Foo is used. However, you may want a .ToXml() method in another assembly and only for that assembly. In that case, you can add the necessary System.Xml and/or System.Xml.Linq dependencies in that assembly and not in the original assembly.

Benefits: dependencies in your defining class assembly is reduced to only the bare necessities and other consuming assemblies will be prevented from using the ToXml() method. See this PDC presentation for further reference.

Reborn answered 28/1, 2009 at 14:56 Comment(0)
N
3

Its true that you can add your (extension) method directly into your class. But not all classes are written by you. Classes from the core library or third party libraries are often closed and it would be impossible to get the syntatic sugar without extension methods. But remember, extension methods are just like (static) standalone methods in eg. c++

Nighthawk answered 28/1, 2009 at 14:56 Comment(0)
R
2

I use them to reuse my object model classes. I have a bunch of classes that represent objects that I have in a database. These classes are used in the client side only to display the objects so the basic usage is accessing properties.

public class Stock {
   public Code { get; private set; }
   public Name { get; private set; }
}

Because of that usage pattern I don't want to have business logic methods in these classes, so I make every business logic to be an extension method.

public static class StockExtender {
    public static List <Quote> GetQuotesByDate(this Stock s, DateTime date)
    {...}
}

This way I can use the same classes for business logic processing and for user interface displaying without overloading the client side with unnecessary code.

One interesting thing about this solution it's that my object model classes are dynamic generated using Mono.Cecil, so it would be very difficult to add business logic methods even if I wanted. I have a compiler that reads XML definition files and generate these stubs classes representing some object I have in the database. The only approach in this case is to extend them.

Rutland answered 28/1, 2009 at 14:56 Comment(1)
You could use partial classes for this...Hieronymus
G
2
  • Intellisense on the object itself instead of having to call some ugly utility function
  • For conversion functions, can change "XToY(X x)" to "ToY(this X x)" which results in pretty x.ToY() instead of ugly XToY(x).
  • Extend classes you have no control over
  • Extend functionality of classes when its undesirable to add methods to the classes themselves. For example, you can keep business objects simple and logic-free, and add specific business logic with ugly dependencies in extension methods
Gass answered 28/1, 2009 at 14:56 Comment(0)
V
2

I mainly see extension methods as an admission that perhaps they shouldn't have disallowed free functions.

In the C++ community, it is often considered good OOP practice to prefer free nonmember functions over members, because these functions don't break encapsulation by gaining access to private members they don't need. Extension methods seem to be a roundabout way to achieve the same thing. That is, a cleaner syntax for static functions which don't have access to private members.

Extension methods are nothing more than syntactic sugar, but I don't see any harm in using them.

Video answered 28/1, 2009 at 14:56 Comment(0)
W
2

Extension methods are really the .NET incorporation of the "Introduce Foreign Method"refactor from Martin Fowler's Book (down to the method signature). They come with basically the same benefits and pitfalls. In the section on this refactor he says that they're a work-around for when you can't modify the class that should really own the method.

Womanish answered 28/1, 2009 at 14:56 Comment(1)
+1, but note that you can use extension methods with interfaces as well.Horus
V
1

Also remember that extension methods were added as a way to help Linq query to be more readable, when used in their C# style.

These 2 affectations are absolutely equivalent, yet the first is far more readable (and the gap in readibility would of course increase with more methods chained).

int n1 = new List<int> {1,2,3}.Where(i => i % 2 != 0).Last();

int n2 = Enumerable.Last(Enumerable.Where(new List<int> {1,2,3}, i => i % 2 != 0));

Note that the fully qualified syntax should even be :

int n1 = new List<int> {1,2,3}.Where<int>(i => i % 2 != 0).Last<int>();

int n2 = Enumerable.Last<int>(Enumerable.Where<int>(new List<int> {1,2,3}, i => i % 2 != 0));

By chance, the type parameters of Where and Last don't need to be explicitely mentioned as they can be infered thanks to the presence of the first parameter of these two methods (the parameter which is introduced by the keyword this and make them extension methods).

This point is obviously an advantage (among others) of the extension methods, and you can take benefit from it in every similar scenario where method chaining is involved.

Especially, it is the more elegant and convincing way I found to have a base class method invokable by any subclass and returning a strongly typed reference to this subclass (with the subclass type).

Example (ok, this scenario is totally cheesy) : after a good night, an animal opens the eyes then gives a cry; every animal opens the eyes the same way, whereas a dog barks and a duck kwaks.

public abstract class Animal
{
    //some code common to all animals
}

public static class AnimalExtension
{
    public static TAnimal OpenTheEyes<TAnimal>(this TAnimal animal) where TAnimal : Animal
    {
        //Some code to flutter one's eyelashes and then open wide
        return animal; //returning a self reference to allow method chaining
    }
}

public class Dog : Animal
{
    public void Bark() { /* ... */ }
}

public class Duck : Animal
{
    public void Kwak() { /* ... */ }
}

class Program
{
    static void Main(string[] args)
    {
        Dog Goofy = new Dog();
        Duck Donald = new Duck();
        Goofy.OpenTheEyes().Bark(); //*1
        Donald.OpenTheEyes().Kwak(); //*2
    }
}

Conceptually OpenTheEyes should be an Animal method, but it would then return an instance of the abstract class Animal, which doesn't know specific subclass methods like Bark or Duck or whatever. The 2 lines commented as *1 and *2 would then raise a compile error.

But thanks to the extension methods, we can have kind of a "base method which knows the subclass type on which it is called".

Note that a simple generic method could have done the job, but in a far more awkward way :

public abstract class Animal
{
    //some code common to all animals

    public TAnimal OpenTheEyes<TAnimal>() where TAnimal : Animal
    {
        //Some code to flutter one's eyelashes and then open wide
        return (TAnimal)this; //returning a self reference to allow method chaining
    }
}

This time, no parameter and thus no possible return type inference. The call can be nothing other than :

Goofy.OpenTheEyes<Dog>().Bark();
Donald.OpenTheEyes<Duck>().Kwak();

... which can weigh the code a lot if more chaining is involved (especially knowing that the type parameter will always be <Dog> on Goofy's line and <Duck> on Donald's one...)

Varioloid answered 28/1, 2009 at 14:56 Comment(1)
First time I've ever seen "kwak". Is this a spelling that is common in another country? The only way I've ever seen it spelled is "quack."Fishnet
B
1

Extension methods can be used to create a kind of mixin in C#.

This, in turn, provides better separation of concerns for orthogonal concepts. Take a look at this answer as an example.

This can also be used to enable roles in C#, a concept central to the DCI architecture.

Blender answered 28/1, 2009 at 14:56 Comment(0)
F
1

One case where extension methods were quite useful was in a client-application that uses ASMX web services. Due to the serialization, the return types of web methods do not contain any methods (only the public properties of these types are available on the client).

Extension methods allowed use to add functionality (on the client-side) to the types returned by web methods without having to create yet another object model or numerous wrapper classes on the client-side.

Formate answered 28/1, 2009 at 14:56 Comment(0)
S
1

In my last project, I used extension method to attach Validate() methods to business objects. I justified this because the business objects where serializable data transfer objects and will be used in diffrent domains as they where general ecommerce entities such as product, customer, merchant etc. Well in diffrent domains the business rules may be diffrent as well so I encapsulated my late bound validation logic in a Validate method attahced to the base class of my data transfer objects. Hope this makes sense :)

Soulless answered 28/1, 2009 at 14:56 Comment(0)
M
1

It allows C# to better support dynamic languages, LINQ and a dozen other things. Check out Scott Guthrie's article.

Minnow answered 28/1, 2009 at 14:56 Comment(0)
D
0

There are so many great examples of extension methods..especially on IEnumerables as posted above.

e.g. if I have an IEnumerable<myObject> I can create and extension method for IEnumerable<myObject>

mylist List<myObject>;

...create the list

mylist.DisplayInMyWay();

Without Extension Methods would have to call:

myDisplayMethod(myOldArray); // can create more nexted brackets.

another great example is creating a Circular Linked List in a flash!

I can' take credit for it!

circlular linked list using extension Methods

Now combine these and using extension Methods code reads as follows.

myNode.NextOrFirst().DisplayInMyWay();

rather than

DisplayInMyWay(NextOrFirst(myNode)).

using Extension Methods It is neater and easier to read and more object orientated. also very close to :

myNode.Next.DoSomething()

Show that to your collegue! :)

Dalia answered 28/1, 2009 at 14:56 Comment(0)
V
0

I have input zones on my screen, and all must implement a standard behavior whatever their exact types are (textboxes, checkboxes, etc.). They cannot inherit a common base class as each type of input zone already derives from a specific class (TextInputBox, etc.)

Maybe by going up in the inheritance hierachy I could find a common ancestor like say WebControl, but I didn't develop the framework class WebControl and it doesn't expose what I need.

With the extension method, I can :

1) extend the WebControl class, and then obtain my unified standard behavior on all my input classes

2) alternatively make all my classes derive from an interface, say IInputZone, and extend this interface with methods. I will now be able to call extensions methods related to the interface on all my input zones. I thus achieved a kind of multiple inheritance since my input zones already derived from multiple base classes.

Varioloid answered 28/1, 2009 at 14:56 Comment(0)
F
0

I've found extension methods are useful to match nested generic arguments.

That sounds a bit wierd - but say we have a generic class MyGenericClass<TList>, and we know that TList itself is generic (e.g. a List<T>), I don't think that there's a way to dig out that nested 'T' from the List without either extension methods or static helper methods. If we only have static helper methods at our disposal, it's (a) ugly, and (b) will force us to move functionality that belongs in the class to an external location.

e.g. to retrieve the types in a tuple and convert them into a method signature we can use extension methods:

public class Tuple { }
public class Tuple<T0> : Tuple { }
public class Tuple<T0, T1> : Tuple<T0> { }

public class Caller<TTuple> where TTuple : Tuple { /* ... */ }

public static class CallerExtensions
{
     public static void Call<T0>(this Caller<Tuple<T0>> caller, T0 p0) { /* ... */ }

     public static void Call<T0, T1>(this Caller<Tuple<T0, T1>> caller, T0 p0, T1 p1) { /* ... */ }
}

new Caller<Tuple<int>>().Call(10);
new Caller<Tuple<string, int>>().Call("Hello", 10);

That said, I'm not sure where the dividing line should be - when should a method be an extension method, and when should it be a static helper method? Any thoughts?

Fashion answered 28/1, 2009 at 14:56 Comment(0)
S
0

I love them for building html. Frequently there are sections that are used repeatedly, or generated recursively where a function is useful but would otherwise break the flow of the program.

        HTML_Out.Append("<ul>");
        foreach (var i in items)
            if (i.Description != "")
            {
                HTML_Out.Append("<li>")
                    .AppendAnchor(new string[]{ urlRoot, i.Description_Norm }, i.Description)
                    .Append("<div>")
                    .AppendImage(iconDir, i.Icon, i.Description)
                    .Append(i.Categories.ToHTML(i.Description_Norm, urlRoot)).Append("</div></li>");
            }

        return HTML_Out.Append("</ul>").ToString();

There are also situations where an object needs custom logic to be prepared for HTML output- extension methods let you add this functionality without mixing presentation and logic within the class.

Speciation answered 28/1, 2009 at 14:56 Comment(0)
C
0

It allows your editor/IDE do auto-complete suggestion smart.

Click answered 28/1, 2009 at 14:56 Comment(0)
P
0

I think extension methods help to write code that is clearer.

Instead of putting a new method inside your class, as your friend suggested, you put it in the ExtensionMethods namespace. In this way you maintain a logical sense of order to your class. Methods that don't really directly deal with your class won't be cluttering it up.

I feel extension methods make your code clearer and more appropriately organized.

Peggypegma answered 28/1, 2009 at 14:56 Comment(0)
P
0

I have only one word to tell about it: MAINTAINABILITY this is the key for extension methods use

Papilionaceous answered 28/1, 2009 at 14:56 Comment(3)
I think you might need more than one word because I don't understand what this means.Kaifeng
I would actually say that is an argument against extention methods. One of the risks of extention methods is that they can be everywhere, created by everyone. Wildgrow can happen if not used carefully.Grasping
You can find all references easily, when you're using EM. Also, in order to change globally one method, you can do it from one place onlyPapilionaceous

© 2022 - 2024 — McMap. All rights reserved.