Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'this' keyword mean in a method parameter? [duplicate]

namespace System.Web.Mvc.Html
{
    // Summary:
    //     Represents support for HTML in an application.
    public static class FormExtensions
    {
        public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName);
...
    }
}

I have noticed that 'this' object in front of the first parameter in BeginForm method doesn't seem to be accepted as a parameter. Looks like in real BeginForm methods functions as:

BeginForm(string actionName, string controllerName);

omitting the first parameter. But it actually receives that first parameter somehow in a hidden way. Can you please explain me how this structure works. I actually exploring MVC 4 internet Sample. Thank you.

like image 740
InGeek Avatar asked Feb 23 '13 00:02

InGeek


People also ask

What is the this keyword in C# used for?

this (C# Reference) The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method.

What does this keyword do in Java?

The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).

What is this keyword in Java Stackoverflow?

It holds the reference of current object.

Is this keyword necessary in Java?

The call “this ()' is used to call more than one constructor from the same class. Q #2) Is this keyword necessary in Java? Answer: It is necessary especially when you need to pass the current object from one method to another, or between the constructors or simply use the current object for other operations.


2 Answers

This is how extension methods works in C#. The Extension Methods feature allowing you to extend existing types with custom methods. The this [TypeName] keyword in the context of method's parameters is the type that you want to extend with your custom methods, the this is used as a prefix, in your case, HtmlHelper is the type to extend and BeginForm is the method which should extend it.

Take a look at this simple extention method for the string type:

public static bool BiggerThan(this string theString, int minChars)
{
  return (theString.Length > minChars);
}

You can easily use it on string object:

var isBigger = "my string is bigger than 20 chars?".BiggerThan(20);

References:

like image 82
14 revs, 2 users 94% Avatar answered Oct 26 '22 23:10

14 revs, 2 users 94%


Extension Methods:

A "bolt on" way to extend an existing type. They allow you to extend an existing type with new functionality, without having to sub-class or recompile the old type. For instance, you might like to know whether a certain string was a number or not. Or you might want to have the Show() Hide() functionality in ASP.net WebForms for controls.

For Example:

public static class MyExtensionMethods
{
    public static void Show(this Control subject)
    {
        subject.Visible = true;
    }
    public static bool IsNumeric(this string s)
    {
        float output;
        return float.TryParse(s, out output);
    }
}

Edit: For futher information you can see the MSDN documentation at: http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx which was kindly linked by @aush.

I enjoyed reading "C# In Depth" regarding Extension Methods. There is an excerpt available here: http://my.safaribooksonline.com/book/programming/csharp/9781935182474/extension-methods/ch10lev1sec3

You can of course buy the book online or you can just do some research into how it all works under the hood using Google.

like image 23
Jeremy Avatar answered Oct 26 '22 23:10

Jeremy