1

I'm having some trouble with some code in Visual C# for Windows Phone The trouble is not that it does not work, because it does, but I don't understand how =P Within a static class, a static method is created, which gives itself as a parameter:

public static void MethodONe( this Timeline animation )
{
    //this class does not extend the TimeLine class, and is not connected to it in any                   
    //such way.
    animation.MethodTwo( );
}

public static void MethodTwo( this Timeline animation )
{
    someCode( );
}

How is this parameterpassing called, and what does it do, exactly?

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
GeekPeek
  • 1,615
  • 1
  • 20
  • 34
  • possible duplicate of [What are Extension Methods?](http://stackoverflow.com/questions/403539/what-are-extension-methods), [Extension Methods](http://stackoverflow.com/questions/5730013/extension-methods), [Why we use “this” in Extension Methods?](http://stackoverflow.com/questions/2574311/why-we-use-this-in-extension-methods) – Cody Gray - on strike Jan 11 '12 at 12:42

1 Answers1

1

This is a so called extention method to the Timeline object. It adds functionallity without modifying the class itself.

http://msdn.microsoft.com/en-us/library/bb383977.aspx

And in your case the animation parameter is the Timeline object (which is calling the function):

var timeLine = new Timeline();
timeLine.MethodTwo();

So the timeLine object will be passed as the animation parameter into the function. There's a nice article on wikipedia which explains it futher in detail:

http://en.wikipedia.org/wiki/Extension_method

A. Tapper
  • 1,261
  • 8
  • 17
  • "...the parameter this is the Timeline object..." - that's not really correct. `this` is a _modifier_ in that case, not a _parameter_. The parameter is `animation` :-) – Stephan Bauer Jan 11 '12 at 12:41