Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, my question is about "this" keyword's meaning in Extension methods. Here's an example from http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx[^]:

C#
public static class ScottGuExtensions
{
    public static bool IsValidEmailAddress(this string s)
    {
        Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
        return regex.IsMatch(s);
    }
}


In the website it says "this" means that this Extension Method should be applied to object with a string type. What i don't get it is if we didn't put "this" in parameter declaration, it would still mean the same thing. So the difference is lost to me. Any help is welcome.
Posted
Comments
Richard MacCutchan 17-Sep-13 10:38am    
I think if you did not put this in, it would not work. This syntaxt is specific to extension methods.

It is a matter of syntax. The "this" keyword is only marking that you make an extension method for the type of that parameter. But I like this syntax because it is really in line with the keyword's other usage.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Sep-13 10:50am    
Agree, 5ed. The syntax is not casual and is organically related to other OOP principles and syntax.
I added another answer (at the same time), please see.
—SA
With the 'extension' method:
C#
public static class ScottGuExtensions
{
    public static bool IsValidEmailAddress(this string s)
    {
        Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
        return regex.IsMatch(s);
    }
}

you can use the method like that:
C#
string s = "someone@somewhere.com";
if (s.IsValidEmailAddress()) {
   // Whatever
}


With the 'non-extension' method :
C#
public static class ScottGuExtensions
{
    public static bool IsValidEmailAddress(string s)
    {
        Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
        return regex.IsMatch(s);
    }
}

you can use the mode like that:
C#
string s = "someone@somewhere.com";
if (ScottGuExtensions.IsValidEmailAddress(s)) {
   // Whatever
}


Does it make sense?
 
Share this answer
 
No, it won't mean the same thing, not even close. Every parameter counts and make the difference. Without "this" how would you access the object you want to operate on.

You are confused quite well. Probably, all you need it to read on the topic more carefully: http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx[^].

Also, I doubt that you understand the instance and static methods in general, as well as
this<code> reference. Perhaps my past answers could help you:<br />
<a href="http://www.codeproject.com/Answers/207285/Csharp-windows-base-this-key-word-related-and-its#answer2">C# windows base this key word related and its uses in the application</a>[<a href="http://www.codeproject.com/Answers/207285/Csharp-windows-base-this-key-word-related-and-its#answer2" target="_blank" title="New Window">^</a>],<br />
<a href="http://www.codeproject.com/Answers/223846/What-makes-static-methods-accessible#answer1">What makes static methods accessible?</a>[<a href="http://www.codeproject.com/Answers/223846/What-makes-static-methods-accessible#answer1" target="_blank" title="New Window">^</a>].<br />
<br />
<dd>—SA</dd>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900