The 'this' prepended in function parameter with C#
The Real World Functional Programming has this code in page 65.
The Tuple has two properties Item1 and Item2, and it has TupleExtensions class as follows.
static class TupleExtensions {
public static Tuple<T1, T2> WithItems2<T1, T2>
(this Tuple<T1, T2> tuple, T2 newItem2) { // line3 ???
return Tuple.Create(tuple.Item1, newItme2); // line4 ???
}
}
var pragueOld = Tuple.Create("Prague", 1188000);
var pragueNew = pragueOld.WithItem2(pragueOld.Item2 + 13195);
The pragueOld.WithItem2()
has only one parameter, but the definition has two parameters.
I think line 3, 4 is the same as this code.
(T2 newItem2) {
return Tuple.Create(this.Item1, newItem2)
What's the meaning of this
as is prepended in the first parameter? What advantages does this
provides 开发者_如何学编程compared to the simple this.Item1
usage?
the this keyword, in this context, means that you are declaring an extension method.
To declare an extension method you must be in the scope of a static class and the method must also be marked static.
This would be an example
namespace Utils
{
public static class StringExtension
{
public static int NonWhitespaceLength(this string text)
{
return text.Replace(" ","").Length;
}
}
}
and you could call this by either
var a = "hello world ! ! !";
Console.WriteLine(Utils.StringExtension.NonWhitespaceLength(a));
or simply
using Utils.StringExtension;
var a = "hello world ! ! !";
Console.WriteLine(a.NonWhitespaceLength());
an additional note: the "this" parameter can actually be null. Which isn't that intuitive and it took me a while to realize, so you can either expect the function to fail if null is passed, or add null reference checks, like so:
namespace Utils
{
public static class StringExtension
{
public static int NonWhitespaceLength(this string text)
{
return text == null ? 0 : text.Replace(" ","").Length;
}
}
}
In this case method WithItem2 is an extension method. It was added to c# to allow developers add "virtual" methods to existing classes without modifing source code. "this" keyword tells that new method belongs to Tuple type in your case Extension Methods (C# Programming Guide)
This seems to be a very contrived example.
The this
in the argument is the way C# implements an extension (sometimes called a mixin). See http://msdn.microsoft.com/en-us/library/bb383977.aspx
Extensions allow you to add methods for a class that you cannot change; i.e. don't have source code for.
See http://msdn.microsoft.com/en-us/library/bb383977.aspx
Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.
The example given is
namespace ExtensionMethods
{
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}
where the usage would be s.WordCount(). In your Tuple example, the method arguments are (this Tuple tuple, T2 newItem2) so the usage is expectedly t.WithItem2(newItem2).
精彩评论