0

Possible Duplicate:
What are Extension Methods?

I know this questions has been asked previously, but could some provide a non-techy explanation, as simple as possible in laymens terms.

All of documentation on other answers seems to be a little far out for me

Community
  • 1
  • 1
Michael
  • 125
  • 4
  • @Michael: I have voted to close the question. See the link in the above comment, which explains the usage of it & I am sure, you'll find it simple. – shahkalpesh May 17 '11 at 15:47
  • @Michael: And if you don't, please feel free to leave comments on the answers for clarifications! – adamjford May 17 '11 at 15:49
  • @shahkalpesh: I agree. If that link doesn't explain extension methods sufficiently, then maybe extension methods aren't the way to go ;) – Town May 17 '11 at 15:50
  • It's just not possible to explain this in laymen's terms. And if you are a layman, you don't need to know what extension methods are, so there's no problem. – R. Martinho Fernandes May 17 '11 at 15:52
  • @Michael: Imagine a dog. You can't change the genetic make-up of a dog (well, you could, but it'd be hard work) but you can give it some roller skates to make it go faster. The roller skates are the extension method - they don't change the dog, they just make it better. – Town May 17 '11 at 15:56
  • @Town Thank you, I think I have been on the wrong wave legnth, my understnading of an extension method now is: that is a way of extending a type, by means of using a static method – Michael May 17 '11 at 16:03
  • @Michael: I prefer the dog analogy personally, but that'll do too ;) They're really not that complicated - have a read of the answer in the duplicate, I'm sure it'll become clear. – Town May 17 '11 at 16:13

5 Answers5

2

Extension methods are a way of simulating new methods on a type without actually changing the type definition itself.

I guess a layman way of explaining it is that it gives every type it's own personal entourage. They person itself is not modified they just gain a host of new abilities simply by virtue of the people who are paid to hang out with them.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • +1 for the non-technical analogy, although I had to read that more times than the documentation on extension methods ;) – Town May 17 '11 at 15:53
1

I don't think it gets much simpler than the one sentence from the Wikipedia article:

"Extension methods enable you to 'add' methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type."

Tim
  • 14,999
  • 1
  • 45
  • 68
0

Well, programming is inherently "techy", so I would try to learn as much as you can in order to understand the documentation. However, extension methods simply allow you to add methods that act like instance methods to an existing class that would otherwise be closed for modification.

For example, if I wrote a library that included a type Foo and did not distribute the source code you would be stuck subclassing it to add any functionality, and that would only be possible if I left the type "unsealed". With the advent of extension methods you are able to add methods to the class that you can call as you would any other method (in reality they are implemented as static methods that take a hidden first parameter to an instance of Foo, so you still don't have access to private members of the class).

Ed S.
  • 122,712
  • 22
  • 185
  • 265
0

Extension methods allow you to add functionality (methods) to classes you have no access to their source.

atoMerz
  • 7,534
  • 16
  • 61
  • 101
0

You can define a simple class:

public class A
{
    public void B()
    {
        Console.WriteLine("B called");
        this.C();
    }
    public void C()
    {
        Console.WriteLine("C called");
    }
}

But what if you get A defined as:

public class A
{
    public void C()
    {
        Console.WriteLine("C called");
    }
}

And you want to add function B to it? You use extension methods to do it:

public class A
{
    public void C()
    {
        Console.WriteLine("C called");
    }
}
// the extensions class, can be any name, must be public
public class Extensions
{
    public static void B(    this                   A                 me)
 // ^ must be public static  ^ indicates extension  ^ type to extend  ^ name of variable instead of this
    {
        Console.WriteLine("B called");
        // instead of this, you use the name of variable you used in the parameters
        me.C();
    }
}

Now you can call A.B() as it was in the first example.

Daniel
  • 30,896
  • 18
  • 85
  • 139