ExtensionMethod.NET Home of 820 C#, Visual Basic, F# and Javascript extension methods

Return<TIn, TOut>

A 'fluent' logic extension method that takes a value (can be anything) and a function that returns another value (can be anything) based on its logic. This is useful for both evaluating and optionally returning a value without declaring a temporary variable for the value.

Source

/// <summary>
/// Returns a value based on an provided value and evaluation function
/// </summary>
public static TOut Return<TIn, TOut>(
    this TIn value, 
    Func<TIn, TOut> evaluateFunc)
{
    return evaluateFunc(value);
}

Example

return foo.Bar().Return( bar => bar.IsBaz 
   ? (Baz)bar : Baz.Empty );

// Alternative to:

var bar = foo.Bar();
return bar.IsBaz ? (Baz)bar : Baz.Empty;

Author: James White

Submitted on: 5 jan 2011

Language: C#

Type: System.Object

Views: 4230