-1

I have two functions :

public static string Image(this HtmlHelper helper, string src, string alt)

and

public static string Image(HtmlHelper helper, string src, string alt)

And i'd like to know the role of this in the first function and why in this case i have to put the parameter with this in the first place?

Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191

5 Answers5

2

Not sure why you are getting downvotes. People need to learn, right?

Those are Extensions, and they have to go into a static class.

For your HtmlHelper object, you could use it with an Extension like this:

string retString = objHelper.Image(srcString, altString);

If you did not have the Extension, you would have to write:

string retString = Image(objHelper, scrString, altString);

They both produce the same result.

  • Because people also need to search before asking. The very same question has already been asked multiple times. – ken2k Jun 11 '13 at 14:53
  • If you don't know the `this` keyword is for Extensions, what do you search for? `this` comes up with a lot of hits. –  Jun 11 '13 at 14:57
  • Just google anything like "c# this keyword parameter" or "c# this keyword method" and you'll find tons of answers and links about extension methods. It's easy to find an answer, and just so more quicker than writing a question. – ken2k Jun 11 '13 at 14:59
1

The keyword this is making it an extension method against the HtmlHelper class. This is leveraged a lot in MVC where you do something like @Html.Image using the Razor syntax. You might recognize one like this:

@Html.TextBoxFor(...)
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • 2
    Who's the one downvoting answer, then undoing the downvote right thereafter? Be responsible when you choose to downvote answers, don't just do it on a whim. – Nolonar Jun 11 '13 at 14:48
1

It's an extention function call. If you put a this infront of the first parameter, you can call the method as though it were a function of that parameter. It's worth reading up on http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

They can make code so much easier for the next person to read

Mikey Mouse
  • 2,968
  • 2
  • 26
  • 44
1

You have this function:

public static string Image(this HtmlHelper helper, string src, string alt);

This is an Extension Method on HtmlHelper class instance

Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

  • The first parameter of the method specifies the type that the method operates on;
  • It must be preceded with the this modifier as it is an Extension Method.

From MSDN, "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."

Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
  • In BOLD, you say that both of those definitions are Extension Methods, but your text afterwards states an Extension Method must be preceded with the `this` modifier. Some clarification would help. –  Jun 11 '13 at 16:10
  • @Downvoter: Care to comment? – Bhushan Firake Jun 11 '13 at 16:41
  • I don't see a downvote, just one (+1) and zero (-1). Perhaps they took it away. –  Jun 11 '13 at 21:31
0

Thank you guys. this example clears everything:

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);
Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
  • 1
    Yeah, it can be handy for strings. But there are so many other uses for it. If you need some examples, just look at the Linq Library – Mikey Mouse Jun 11 '13 at 15:05