Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>They're static methods that can be added to other types without modifying the original type.</p> <p>For example, say you want to be able to call ToInt() on a string. You can create a method like this:</p> <pre><code>public static int ToInt( this string value ){ return Int32.Parse( value ); } </code></pre> <p>Now say you have a TextBox called Port on your web page you can get it as an int like this:</p> <pre><code>int port = Port.Text.ToInt(); </code></pre> <p>Real world use of extension methods is usually much more useful then that and is used heavily by <a href="http://en.wikipedia.org/wiki/Fluent_interface" rel="nofollow noreferrer">Fluent</a> APIs and <a href="http://www.nunit.org/index.php" rel="nofollow noreferrer">testing</a> frameworks.</p> <p><strong>UPDATE:</strong> Another good use of extensions is to provide base class like default behaviors for interfaces. Many popular testing methodologies depend on interface definitions and the ability to mock those interfaces. Unfortunately, basing everything on interfaces however loses some of the benefits of inheritance. Extension methods can add some of the convenience of base classes without the pollution of the interfaces. </p> <p>For example say I have a <code>Window</code> base class defined like this</p> <pre><code>public class Window { public void Show( object owner ){...} public void Show(){ Show( null ); } } </code></pre> <p>Now I want to extract an <code>IWindow</code> interface from this class. I don't really want to require every object that derives from <code>IWindow</code> to have to implement the second <code>Show</code> method since it's just a basic overload. So I can define my interface like this:</p> <pre><code>public interface IWindow { void Show( object owner ); } </code></pre> <p>And an extension method like this:</p> <pre><code>public static void Show( this IWindow window ) { window.Show( null ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload