11

I've been watching the ASP.NET MVC Storefront video series again and saw something that I've never noticed or payed any attention to before. I noticed there were a lot of references to this in the signature lists of various methods. Here is an example of one:

public static Category WithCategoryName(this IList<Category> list, string categoryName)   
{
    return 
    (
        from s in list
        where s.Name.Equals(categoryName, StringComparison.InvariantCultureIgnoreCase)
        select s
    )
    .SingleOrDefault();
}

I immediately understand the IList<Category> list and the string categoryName in the signature, but was confused about what this does.

So, being a 95% VB guy, I popped the code into my favorite converter and got:

<System.Runtime.CompilerServices.Extension>
Public Shared Function WithCategoryName(list As IList(Of Category), categoryName As String) As Category

    Return 
    (
        From s In list 
        Where s.Name.Equals(categoryName, StringComparison.InvariantCultureIgnoreCase)
        Select s
    )
    .SingleOrDefault()

End Function

First of all, I'm not totally sure why <System.Runtime.CompilerServices.Extension> was included, maybe it's just the converter, nevertheless, as you can see, this wasn't converted into anything that I can tell unless it has to do with the aforementioned <System.Runtime.CompilerServices.Extension>.

So the questions are:

  1. What does this actually refer to and/or do in the C# method signature?
  2. Is there a VB.NET equivalent?



Response to Question 1:

So we've definitely clarified that this does in fact denote an extension method and that from the answers given, it seems there's no inline VB equivalent.

I would like to add that since I mentioned the ASP.NET MVC Storefront video, the C# example above was pulled from his CategoryFilters class. I assume this is how you implement what was referenced as a pipes and filters or pipeline methodology.



Response to Question 2:

I assume VB.NET's way of handling extension methods is something like this for example:

Imports System.Runtime.CompilerServices 

Public Module StringExtensions 

    <Extension()> _ 
    Public Function IsNullOrBlank(ByVal s As String) As Boolean 
       Return s Is Nothing OrElse s.Trim.Length.Equals(0) 
    End Function 

End Module
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
  • http://stackoverflow.com/questions/972433/what-does-this-mean-when-used-as-a-prefix-for-method-parameters – AakashM Apr 19 '12 at 15:29
  • In regards to your edit, VB.NET assumes the first parameter's type is the type you are extending. – duraz0rz Apr 19 '12 at 16:09

3 Answers3

11

That is an extension method. The this specifies that it is an extension method of this <parameter> type, in your case, IList<Category>.

There is a VB.NET equivalent here, though it is an attribute, not a keyword.

Extension methods need to know the type to apply to, note that this is apparent with generics. An extension method:

public static string GetNameOf(this List<Category> category) { return ""; }

Will not be available on anything other than List<Category>.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
7

this appearing in that place means an Extension Method.

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' }, 
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }   
}

after this code any string object in your program can use this function, like

int count = "Hello world".WordCount();  //count would be equal 2

In other words this is a way to extend the functionality of the types of which you do not have access or not allowed to change or derive from.

Tigran
  • 61,654
  • 8
  • 86
  • 123
6

This creates an extension method.

VB.Net doesn't have a corresponding syntax for this, so you need to apply the attribute yourself.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Oh ok, ok. I'm on the same page. VB's equivalent is not really an inline equivalent, but in the sense, you can create a Module class that groups a set of extensions that latch onto whatever class you want. For example: `Imports System.Runtime.CompilerServices Public Module StringExtensions _ Public Function IsNullOrBlank(ByVal s As String) As Boolean Return s Is Nothing OrElse s.Trim.Length.Equals(0) End Function End Module` – Code Maverick Apr 19 '12 at 15:36
  • Exactly. (Note that .Net already has `String.IsNullOrWhitespace`) – SLaks Apr 19 '12 at 15:41
  • Right, I know that, but in my simple extensions module I have I don't want to pass parameters, I wanted it to chain off of any existing string. – Code Maverick Apr 19 '12 at 15:43
  • Thanks for the first and correct response, but for future visitors' sake, I'm going to mark @AdamHouldsworth's answer as the answer due to his explanation and linking to the VB equivalent. – Code Maverick Apr 19 '12 at 15:48