Category Archives: 14279

Extension Methods And Type Inference In Action

I make extensive use of extension methods, either to make classes small and focused or to improve readability.

While porting a .NET 1.1 WinForms application to C# 3.0, I found lots of code like this:

delegate int Int32DelegateStringBoolean(string text, bool flag);

void DoStuff()
{
    // ...

    var x = (int)this.Invoke(new Int32DelegateStringBoolean(GetStuff), new object[] { "some text", true });

    // ...
}

int GetStuff(string text, bool flag)
{
    // ...
}


.NET 2.0 introduced a nicer API and it became possible to write the code calling Invoke like this:



var x = (int)this.Invoke(new Int32DelegateStringBoolean(GetStuff), "some text", true);


But it’s still not strongly typed enough to my taste and the compiler can’t verify if any mistake was made. This will still be valid code at compile time that will break at run time:



var x = (long)this.Invoke(new Int32DelegateStringBoolean(GetStuff), "some text", 10M, 5);


To make the code safer and more readable, I decided to create extension methods to extend the Control class and provide strongly type Invoke methods.



Instead of defining custom delegates for each need, I used the Action and Func delegates exiting in the framework and wrote extension methods like this:



public static TResult InvokeFunc<T1, T2, TResult>(this Control control, Func<T1, T2, TResult> func, T1 param1, T2 param2)
{
    return (TResult)(control.Invoke(func, param1, param2));
}


Now I can replace the call to Invoke with this call:



var x = this.InvokeFunc<string, bool, int>(new Func<string, bool, int>(GetStuff), "some text", true);


And the compiler is now able to match the type of the delegate, the parameters and the return value.



Starting with the C# 2.0 compiler, there is no need to write the delegate instantiation if the compiler can infer the type of the delegate, which makes the code even simpler:



var x = this.InvokeFunc<string, bool, int>(GetStuff, "some text", true);


The C# compiler is even capable of inferring the type parameters of the InvokeFunc method making the code even smaller and more readable:



var x = this.InvokeFunc(GetStuff, "some text", true);


A lot better than what we started with, isn’t it?



So far, I’ve implemented these:



/// <summary>
/// Provides extended functionality to <see cref="System.Windows.Forms.Control"/>.
/// </summary>
public static class ControlExtensions
{
    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to invoke the function on.</param>
    /// <param name="func">The function to invoke.</param>
    /// <returns>A <typeparamref name="TResult"/> that contains the return value from the function (<paramref name="func"/>) being invoked.</returns>
    public static TResult InvokeFunc<TResult>(this Control control, Func<TResult> func)
    {
        return (TResult)(control.Invoke(func));
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to invoke the function on.</param>
    /// <param name="func">The function to invoke.</param>
    /// <param name="param1">The parameter of the action.</param>
    /// <returns>A <typeparamref name="TResult"/> that contains the return value from the function (<paramref name="func"/>) being invoked.</returns>
    public static TResult InvokeFunc<T1, TResult>(this Control control, Func<T1, TResult> func, T1 param1)
    {
        return (TResult)(control.Invoke(func, param1));
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to invoke the function on.</param>
    /// <param name="func">The function to invoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <returns>A <typeparamref name="TResult"/> that contains the return value from the function (<paramref name="func"/>) being invoked.</returns>
    public static TResult InvokeFunc<T1, T2, TResult>(this Control control, Func<T1, T2, TResult> func, T1 param1, T2 param2)
    {
        return (TResult)(control.Invoke(func, param1, param2));
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to invoke the function on.</param>
    /// <param name="func">The function to invoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <param name="param3">The third parameter of the function.</param>
    /// <returns>A <typeparamref name="TResult"/> that contains the return value from the function (<paramref name="func"/>) being invoked.</returns>
    public static TResult InvokeFunc<T1, T2, T3, TResult>(this Control control, Func<T1, T2, T3, TResult> func, T1 param1, T2 param2, T3 param3)
    {
        return (TResult)(control.Invoke(func, param1, param2, param3));
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the function.</typeparam>
    /// <typeparam name="T4">The type of the forth parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to invoke the function on.</param>
    /// <param name="func">The function to invoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <param name="param3">The third parameter of the function.</param>
    /// <param name="param4">The forth parameter of the function.</param>
    /// <returns>A <typeparamref name="TResult"/> that contains the return value from the function (<paramref name="func"/>) being invoked.</returns>
    public static TResult InvokeFunc<T1, T2, T3, T4, TResult>(this Control control, Func<T1, T2, T3, T4, TResult> func, T1 param1, T2 param2, T3 param3, T4 param4)
    {
        return (TResult)(control.Invoke(func, param1, param2, param3, param4));
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <param name="control">The control to invoke the action on.</param>
    /// <param name="action">The action to invoke.</param>
    public static void InvokeAction(this Control control, Action action)
    {
        control.Invoke(action);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T">The type of the parameter.</typeparam>
    /// <param name="control">The control to invoke the action on.</param>
    /// <param name="action">The action to invoke.</param>
    /// <param name="param">The parameter of the action.</param>
    public static void InvokeAction<T>(this Control control, Action<T> action, T param)
    {
        control.Invoke(action, param);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <param name="control">The control to invoke the action on.</param>
    /// <param name="action">The action to invoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    public static void InvokeAction<T1, T2>(this Control control, Action<T1, T2> action, T1 param1, T2 param2)
    {
        control.Invoke(action, param1, param2);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the action.</typeparam>
    /// <param name="control">The control to invoke the action on.</param>
    /// <param name="action">The action to invoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    /// <param name="param3">The third parameter of the action.</param>
    public static void InvokeAction<T1, T2, T3>(this Control control, Action<T1, T2, T3> action, T1 param1, T2 param2, T3 param3)
    {
        control.Invoke(action, param1, param2, param3);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the action.</typeparam>
    /// <typeparam name="T4">The type of the forth parameter of the action.</typeparam>
    /// <param name="control">The control to invoke the action on.</param>
    /// <param name="action">The action to invoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    /// <param name="param3">The third parameter of the action.</param>
    /// <param name="param4">The forth parameter of the action.</param>
    public static void InvokeAction<T1, T2, T3, T4>(this Control control, Action<T1, T2, T3, T4> action, T1 param1, T2 param2, T3 param3, T4 param4)
    {
        control.Invoke(action, param1, param2, param3, param4);
    }
    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to BeginInvoke the function on.</param>
    /// <param name="func">The function to BeginInvoke.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeFunc<TResult>(this Control control, Func<TResult> func)
    {
        return control.BeginInvoke(func);
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to BeginInvoke the function on.</param>
    /// <param name="func">The function to BeginInvoke.</param>
    /// <param name="param1">The parameter of the action.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeFunc<T1, TResult>(this Control control, Func<T1, TResult> func, T1 param1)
    {
        return control.BeginInvoke(func, param1);
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to BeginInvoke the function on.</param>
    /// <param name="func">The function to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeFunc<T1, T2, TResult>(this Control control, Func<T1, T2, TResult> func, T1 param1, T2 param2)
    {
        return control.BeginInvoke(func, param1, param2);
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to BeginInvoke the function on.</param>
    /// <param name="func">The function to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <param name="param3">The third parameter of the function.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeFunc<T1, T2, T3, TResult>(this Control control, Func<T1, T2, T3, TResult> func, T1 param1, T2 param2, T3 param3)
    {
        return control.BeginInvoke(func, param1, param2, param3);
    }

    /// <summary>
    /// Executes the specified function (<paramref name="func"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the function.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the function.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the function.</typeparam>
    /// <typeparam name="T4">The type of the forth parameter of the function.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="control">The control to BeginInvoke the function on.</param>
    /// <param name="func">The function to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the function.</param>
    /// <param name="param2">The second parameter of the function.</param>
    /// <param name="param3">The third parameter of the function.</param>
    /// <param name="param4">The forth parameter of the function.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeFunc<T1, T2, T3, T4, TResult>(this Control control, Func<T1, T2, T3, T4, TResult> func, T1 param1, T2 param2, T3 param3, T4 param4)
    {
        return control.BeginInvoke(func, param1, param2, param3, param4);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <param name="control">The control to BeginInvoke the action on.</param>
    /// <param name="action">The action to BeginInvoke.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeAction(this Control control, Action action)
    {
        return control.BeginInvoke(action);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T">The type of the parameter.</typeparam>
    /// <param name="control">The control to BeginInvoke the action on.</param>
    /// <param name="action">The action to BeginInvoke.</param>
    /// <param name="param">The parameter of the action.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeAction<T>(this Control control, Action<T> action, T param)
    {
        return control.BeginInvoke(action, param);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <param name="control">The control to BeginInvoke the action on.</param>
    /// <param name="action">The action to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeAction<T1, T2>(this Control control, Action<T1, T2> action, T1 param1, T2 param2)
    {
        return control.BeginInvoke(action, param1, param2);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the action.</typeparam>
    /// <param name="control">The control to BeginInvoke the action on.</param>
    /// <param name="action">The action to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    /// <param name="param3">The third parameter of the action.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeAction<T1, T2, T3>(this Control control, Action<T1, T2, T3> action, T1 param1, T2 param2, T3 param3)
    {
        return control.BeginInvoke(action, param1, param2, param3);
    }

    /// <summary>
    /// Executes the specified action (<paramref name="action"/>) on the thread that owns the control's underlying window handle.
    /// </summary>
    /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
    /// <typeparam name="T2">The type of the second parameter of the action.</typeparam>
    /// <typeparam name="T3">The type of the third parameter of the action.</typeparam>
    /// <typeparam name="T4">The type of the forth parameter of the action.</typeparam>
    /// <param name="control">The control to BeginInvoke the action on.</param>
    /// <param name="action">The action to BeginInvoke.</param>
    /// <param name="param1">The first parameter of the action.</param>
    /// <param name="param2">The second parameter of the action.</param>
    /// <param name="param3">The third parameter of the action.</param>
    /// <param name="param4">The forth parameter of the action.</param>
    /// <returns>An System.IAsyncResult that represents the result of the operation.</returns>
    public static IAsyncResult BeginInvokeAction<T1, T2, T3, T4>(this Control control, Action<T1, T2, T3, T4> action, T1 param1, T2 param2, T3 param3, T4 param4)
    {
        return control.BeginInvoke(action, param1, param2, param3, param4);
    }
}


Use them if you need to.

LINQ: Implementing The SkipLastWhile Operator

LINQ Com C#

Following my last posts (>)(>), in this post I’ll introduce the implementation of the SkipLastWhile operator.

The SkipLastWhile returns all but the last contiguous elements from a a sequence that satisfy the specified criteria and is implemented as the SkipLastWhile extension methods:

public static IEnumerable<TSource> SkipLastWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)


public static IEnumerable<TSource> SkipLastWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)


The implementation of these methods is very simple. We start with an empty buffer and buffer every item that satisfies the criteria implemented by a predicate. Whenever an item doesn’t satisfy the criteria, all buffered items are yield, the buffer is cleared and the the item that doesn’t satisfy the criteria is yield:



var buffer = new List<TSource>();

foreach (var item in source)
{
    if (predicate(item))
    {
        buffer.Add(item);
    }
    else
    {
        if (buffer.Count > 0)
        {
            foreach (var bufferedItem in buffer)
            {
                yield return bufferedItem;
            }

            buffer.Clear();
        }

        yield return item;
    }
}


The overload that takes in account the index of the item only differs in the call the predicate that implements the criteria:



var buffer = new List<TSource>();
var idx = 0;

foreach (var item in source)
{
    if (predicate(item, idx++))
    {
        buffer.Add(item);
    }
    else
    {
        if (buffer.Count > 0)
        {
            foreach (var bufferedItem in buffer)
            {
                yield return bufferedItem;
            }

            buffer.Clear();
        }

        yield return item;
    }
}



You can find the complete implementation of this operator (and more) CodePlex project for LINQ utilities and operators: PauloMorgado.Linq

LINQ: Implementing The SkipLast Operator

LINQ With C# (Portuguese)

Following my last post, in this post I’ll introduce the implementation of the SkipLast operator.

The SkipLast operator returns all but a specified number of contiguous elements from the end of a sequence and is implemented as the SkipLast extension method:

public static IEnumerable<TSource> SkipLast<TSource>(this IEnumerable<TSource> source, int count)


To implement this operator, first we start by buffering, at most, a count number of items from the source sequence in an array that acts as a circular buffer:



var sourceEnumerator = source.GetEnumerator();
var buffer = new TSource[count];
int idx;

for (idx = 0; (idx < count) && sourceEnumerator.MoveNext(); idx++)
{
    buffer[idx] = sourceEnumerator.Current;
}


Next, we iterate over the rest of the items of the source sequence circularly buffering them and yielding the previously buffered item at the same postition:



idx = 0;

while (sourceEnumerator.MoveNext())
{
    var item = buffer[idx];

    buffer[idx] = sourceEnumerator.Current;

    idx = (idx + 1) % count;

    yield return item;
}


If the number of items to skip is greater or equal to the number of items in the source sequence, sourceEnumerator.MoveNext() will return false on the first iteration of the while loop and an empty sequence will be produced.



As with the TakeLast operator, a few optimizations can be made here.



The first is obvious, if the requested number of items is 0 (zero) or lower, we just return an equivalent sequence:



if (count <= 0)
{
    return source.Select(i => i);
}


The second is if the source sequence is known to implement the IList<T> interface. Objects implementing this interface have a Count property and indexed access to its items which allows us to optimize the production of the final sequence.



If the number of items to skip is equal to or greater than the number of items in the source sequence, we just return an empty sequence:



var list = source as IList<TSource>;

if (list != null)
{
    if (count >= list.Count)
    {
        return System.Linq.Enumerable.Empty<TSource>();
    }

    // ...
}


If the number of items in the source sequence is greater than the number of items to skip, producing the final sequence consists of yielding all the items in the source sequence except the last count items:



int returnCount = list.Count - count;

for (int idx = 0; idx < returnCount; idx++)
{
    yield return list[idx];
}


You can find the complete implementation of this operator (and more) CodePlex project for LINQ utilities and operators: PauloMorgado.Linq

LINQ: Introducing The Skip Last Operators

LINQ With C# (Portuguese)

After having introduced the TakeLast operators (>)(>)(>), it makes sense to introduce their duals: the SkipLast operators.

Name Description Example
SkipLast<TSource>(IEnumerable<TSource>)

Returns all but a specified number of contiguous elements from the end of a sequence.

int[] grades = { 59, 82, 70, 56, 92, 98, 85 };

var lowerGrades = grades
                  .OrderBy(g => g)
                  .SkipLast(3);

Console.WriteLine("All grades except the top three are:");
foreach (int grade in lowerGrades)
{
    Console.WriteLine(grade);
}

/*
This code produces the following output:

All grades except the top three are:
56
59
70
82
*/
SkipLastWhile<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)

Returns all the elements from sequence skipping those at the end as long as the specified condition is true.

int[] grades = { 59, 82, 70, 56, 92, 98, 85 };

var lowerGrades = grades
                 .OrderBy(grade => grade)
                 .SkipLastWhile(grade => grade >= 80);

Console.WriteLine("All grades below 80:");
foreach (int grade in lowerGrades)
{
    Console.WriteLine(grade);
}

/*
This code produces the following output:

All grades below 80:
56
59
70
*/
SkipLastWhile<TSource>(IEnumerable<TSource>, Func<TSource, Int32, Boolean>)

Returns all the elements from sequence skipping those at the end as long as the specified condition is true.

int[] amounts =
    {
        5000,
        2500,
        5500,
        8000,
        6500,
        4000,
        1500,
        9000
    };

var query = amounts
            .SkipLastWhile((amount, index) => amount > index * 1000);

foreach (int amount in query)
{
    Console.WriteLine(amount);
}

/*
 This code produces the following output:

9000
*/


You can find these (and more) operators in my CodePlex project for LINQ utilities and operators: PauloMorgado.Linq

LINQ: Implementing The TakeLastWhile Operator

LINQ With C# (Portuguese)

Following my last posts (>)(>), in this post I’ll introduce the implementation of the TakeLastWhile operator.

The TakeLastWhile operator returns last contiguous elements from a sequence that satisfy the specified criteria and is implemented as the TakeLastWhile extension methods:

public static IEnumerable<TSource> TakeLastWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)


public static IEnumerable<TSource> TakeLastWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)


The implementation of the first method is very simple. We start with an empty buffer and buffer every item that satisfies the criteria implemented by a predicate. Whenever an item doesn’t satisfy the criteria, the buffer is cleared:



var buffer = new List<TSource>();

foreach (var item in source)
{
    if (predicate(item))
    {
        buffer.Add(item);
    }
    else
    {
        buffer.Clear();
    }
}


After traversing the source sequence, we just yield all the items, if any, in the buffer:



foreach (var item in buffer)
{
    yield return item;
}


The overload that takes in account the index of the item only differs in the call the predicate that implements the criteria:



var buffer = new List<TSource>();
var idx = 0;

foreach (var item in source)
{
    if (predicate(item, idx++))
    {
        buffer.Add(item);
    }
    else
    {
        buffer.Clear();
    }
}

foreach (var item in buffer)
{
    yield return item;
}


You can find the complete implementation of this operator (and more) CodePlex project for LINQ utilities and operators: PauloMorgado.Linq

LINQ: Implementing The TakeLast Operator

LINQ With C# (Portuguese)

Following my last post, in this post I’ll introduce the implementation of the TakeLast operator.

The TakeLast operator returns a specified number of contiguous elements from the end of a sequence and is implemented as the TakeLast extension method:

public static IEnumerable<TSource> TakeLast<TSource>(this IEnumerable<TSource> source, int count)


To implement this operator, first we start by buffering, at most, a count number of items from the source sequence in an array that acts as a circular buffer:



var sourceEnumerator = source.GetEnumerator();
var buffer = new TSource[count];
var numOfItems = 0;
int idx;

for (idx = 0; (idx < count) && sourceEnumerator.MoveNext(); idx++, numOfItems++)
{
    buffer[idx] = sourceEnumerator.Current;
}


If the number of buffered items (numOfItems) is less than the requested number of items (count), we just yield all the buffered items:



if (numOfItems < count)
{
    for (idx = 0; idx < numOfItems; idx++)
    {
        yield return buffer[idx];
    }

    yield break;
}


Next, we iterate over the rest of the items circularly buffering them:



for (idx = 0; sourceEnumerator.MoveNext(); idx = (idx + 1) % count)
{
    buffer[idx] = sourceEnumerator.Current;
}


And finally, we just iterate over the buffered items and yield them:



for (; numOfItems > 0; idx = (idx + 1) % count, numOfItems--)
{
    yield return buffer[idx];
}


There are two optimizations you can make here.



The first is obvious, if the requested number of items is 0 (zero), we just return an empty sequence:



if (count <= 0)
{
    return System.Linq.Enumerable.Empty<TSource>();
}


The second is if the source sequence is known to implement the IList<T> interface. Objects implementing this interface have a Count property and indexed access to its items which allows us to optimize the production of the final sequence.



Producing the final sequence consists of yielding the required number of items from the end of the list (or all of them if the list contains less items than required):



int listCount = list.Count;

for (int idx = listCount - ((count < listCount) ? count : listCount); idx < listCount; idx++)
{
    yield return list[idx];
}


You can find the complete implementation of this operator (and more) CodePlex project for LINQ utilities and operators: PauloMorgado.Linq

Hydrating Objects With Expression Trees – Part III

LINQ With C# (Portuguese)

To finalize this series on object hydration, I’ll show some performance comparisons between the different methods of hydrating objects.

For the purpose of this exercise, I’ll use this class:

class SomeType
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTimeOffset CreationTime { get; set; }
    public Guid UniqueId { get; set; }
}


and this set of data:



var data = (
    from i in Enumerable.Range(1, ObjectCount)
    select new object[] { i, i.ToString(), DateTimeOffset.Now, Guid.NewGuid() }
).ToArray();


The data bellow shows the time (in seconds) for different runs for different values of ObjectCount (in the same machine with approximately the same load) as well as it’s availability for different version of the .NET Framework and the C# programming language:



10000 100000 1000000 Valid for
Setup Hydrate Total Setup Hydrate Total Setup Hydrate Total Framework version C# Version
Activation and Reflection setter 0.060 0.101 0.161 0.055 0.736 0.791 0.054 6.822 6.876 1.0, 1.1, 2.0, 3.5, 4.0 1.0, 2.0, 3.0, 4.0
Activation and Expression Tree setter 0.300 0.003 0.303 0.313 0.049 0.359 0.293 0.578 0.871 4.0 none
Member Initializer 0.035 0.001 0.036 0.039 0.027 0.066 0.041 0.518 0.559 3.5, 4.0 3.0, 4.0


These values will vary with the number of the objects being hydrated and the number of its properties, but the method using the Member Initializer will be the most performant.



Code samples for this series of posts (and the one about object dumping with expression trees) can be found on my MSDN Code Gallery: Dump And Hydrate Objects With Expression Trees

Hydrating Objects With Expression Trees – Part II

LINQ With C# (Portuguese)

In my previous post I showed how to hydrate objects by creating instances and setting properties in those instances.

But, if the intent is to hydrate the objects from data, why not having an expression that does just that? That’s what the member initialization expression is for.

To create such an expression we need the constructor expression and the property binding expressions:

var properties = objectType.GetProperties();
var bindings = new MemberBinding[properties.Length];
var valuesArrayExpression = Expression.Parameter(typeof(object[]), "v");

for (int p = 0; p < properties.Length; p++)
{
    var property = properties[p];

    bindings[p] = Expression.Bind(
        property,
        Expression.Convert(
            Expression.ArrayAccess(
                valuesArrayExpression,
                Expression.Constant(p, typeof(int))
            ),
            property.PropertyType
        )
    );
}

var memberInitExpression = Expression.MemberInit(
    Expression.New(objectType),
    bindings
);

var objectHidrationExpression = Expression.Lambda<Func<object[], object>>(memberInitExpression, valuesArrayExpression);

var compiledObjectHidrationExpression = objectHidrationExpression.Compile();


This might seem more complex than the previous solution, but using it is a lot more simple:



for (int o = 0; o < objects.Length; o++)
{
    newObjects[o] = compiledObjectHidrationExpression(objects[o]);
}

Mastering Expression Trees With .NET Reflector

Following my last post, I received lots of enquiries about how got to master the creation of expression trees.

The answer is: .NET Reflector

On that post I needed to to generate an expression tree for this expression:

Expression<Func<object, object>> expression = o => ((object)((SomeType)o).Property1);


I just compiled that code in Visual Studio 2010, loaded the assembly in .NET Reflector, and disassembled it to C# without optimizations (View –> Options –> Disassembler –> Optimization: None).



The disassembled code looked like this:



Expression<Func<object, object>> expression;
ParameterExpression CS$0$0000;
ParameterExpression[] CS$0$0001;
expression = Expression.Lambda<Func<object, object>>(Expression.Convert(Expression.Property(Expression.Convert(CS$0$0000 = Expression.Parameter(typeof(object), "o"), typeof(SomeType)), (MethodInfo) methodof(SomeType.get_Property1)), typeof(object)), new ParameterExpression[] { CS$0$0000 });


After giving valid C# names to the variables and tidy up the code a bit, I came up with this:



ParameterExpression parameter = Expression.Parameter(typeof(object), "o");
Expression<Func<object, object>> expression =
    Expression.Lambda<Func<object, object>>(
        Expression.Convert(
            Expression.Property(
                Expression.Convert(
                    parameter,
                    typeof(SomeType)
                ),
                "Property1"
            ),
            typeof(object)
        ),
        parameter
    );


Easy! Isn’t it?

Dumping Objects Using Expression Trees

LINQ With C# (Portuguese)


No. I’m not proposing to get rid of objects.


A colleague of mine was asked if I knew a way to dump a list of objects of unknown type into a DataTable with better performance than the way he was using.


The objects being dumped usually have over a dozen of properties, but, for the sake of this post, let’s assume they look like this:


class SomeClass
{
    public int Property1 { get; set; }
    public long Property2 { get; set; }
    public string Property3 { get; set; }
    public object Property4 { get; set; }
    public DateTimeOffset Property5 { get; set; }
}

The code he was using was something like this:


var properties = objectType.GetProperties();

foreach (object obj in objects)
{
    foreach (var property in properties)
    {
        property.GetValue(obj, null);
    }
}

For a list of one million objects, this is takes a little over 6000 milliseconds on my machine.


I immediately thought: Expression Trees!


If the type of the objects was know at compile time, it would be something like this:


Expression<Func<SomeClass, int>> expression = o => o.Property1;
var compiled = expression.Compile();
var propertyValue = compiled.Invoke(obj);

But, at compile time, the type of the object and, consequently, the type of its properties, is unknown. So, we’ll need, for each property, an expression tree like this:


Expression<Func<object, object>> expression = o => ((SomeClass)o).Property1;

The previous expression gets the value of a property of the conversion of the parameter of type object to the type of the object. The result must also be converted to type object because the type of the result must match the type of the return value of the expression.


For the same type of objects, the collection of property accessors would be built this way:


var compiledExpressions = (from property in properties
                           let objectParameter = Expression.Parameter(typeof(object), "o")
                           select
                             Expression.Lambda<Func<object, object>>(
                                 Expression.Convert(
                                     Expression.Property(
                                         Expression.Convert(
                                             objectParameter,
                                             objectType
                                         ),
                                         property
                                     ),
                                     typeof(object)
                                 ),
                                 objectParameter
                             ).Compile()).ToArray();

Looks bit overcomplicated, but reading all properties of all objects for the same object set with this code:


foreach (object obj in objects)
{
    foreach (var compiledExpression in compiledExpressions)
    {
        compiledExpression (obj);
    }
}

takes a little over 150 milliseconds on my machine.


That’s right. 2.5% of the previous time.