generics and this keyword in argument list

P

puzzlecracker

In the example below ( it's taken off Jon's blogs), what does this
keyword in the parameter list mean?
Also, why do we need to do specify generic T here WithLogging<T>?
wouldn't type be derived from IEnumerable<T> ?


using System;
using System.Collections.Generic;

public static class Extensions
{
public static IEnumerable<T> WithLogging<T>(this IEnumerable<T>
source,
string name)
{
foreach (T element in source)
{
Console.WriteLine("{0}: {1}", name, element);
yield return element;
}
}
}
 
M

Martin Honnen

puzzlecracker said:
In the example below ( it's taken off Jon's blogs), what does this
keyword in the parameter list mean?
Also, why do we need to do specify generic T here WithLogging<T>?
wouldn't type be derived from IEnumerable<T> ?


using System;
using System.Collections.Generic;

public static class Extensions
{
public static IEnumerable<T> WithLogging<T>(this IEnumerable<T>
source,
string name)

It is a static class with a static method using 'this' on the first
argument to define an extension method. So using C# or VB.NET 2008 you
can call a method WithLogging on any IEnumerable<T>.
See http://msdn.microsoft.com/en-us/library/bb383977.aspx for an
introduction to extension methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top