Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sounds like an <a href="http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx" rel="nofollow">Extension Method</a>.</p> <p>The <code>@</code> symbol allows the variable name to be the same as a C# keyword - I tend to avoid them like the plague personally.</p> <p>If you remove the <code>this</code> keyword, it will no longer be an extension method, just a static method. Depending on the calling code syntax, it may no longer compile, for example:</p> <pre><code>public static class IntegerMethods { public static int Add(this int i, int value) { return i + value; } } int i = 0; // This is an "extension method" call, and will only compile against extension methods. i = i.Add(2); // This is a standard static method call. i = IntegerMethods.Add(i, 2); </code></pre> <p>The compiler will simply translate all "extension method calls" into standard static method calls at any rate, but extension method calls will still only work against valid extension methods as per the <code>this type name</code> syntax.</p> <p><strong>Some guidelines</strong></p> <p>These are my own, but I find they are useful.</p> <ul> <li>Discoverability of extension methods can be a problem, so be mindful of the namespace you choose to contain them in. We have <em>very useful</em> stuff under .NET namespaces such as <code>System.Collections</code> or whatever. Less useful but otherwise "common" stuff tends to go under <code>Extensions.&lt;namespace of extended type&gt;</code> such that discoverability is at least consistent via convention.</li> <li>Try not to extend often used types in broad scope, you don't want <code>MyFabulousExtensionMethod</code> appearing on <code>object</code> throughout your app. If you <em>need</em> to, either constrain the scope (namespace) to be <em>very</em> specific, or bypass extension methods and use a static class directly - these won't pollute the type metadata in IntelliSense.</li> <li>In extension methods, "this" can be <code>null</code> (due to how they compile into static method calls) so be careful and don't assume that "this" is not null (from the calling side this looks like a successful method call on a null target).</li> </ul> <p>These are optional and not exhaustive, but I find they usually fall under the banner of "good" advice. YMMV.</p>
    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.
 

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