Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Using extension for small using statements. #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Z.Core/System.Using/TResult.Using.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Description: C# Extension Methods Library to enhances the .NET Framework by adding hundreds of new methods. It drastically increases developers productivity and code readability. Support C# and VB.NET
// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods
// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues
// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE
// More projects: http://www.zzzprojects.com/
// Copyright � ZZZ Projects Inc. 2014 - 2016. All rights reserved.
using System;
using System.Text;

public static partial class Extensions
{
/// <summary>
/// Using statement extension to skip small using blocks.
/// </summary>
/// <param name="factory">The factory function to create disposable object to act on.</param>
/// <param name="action">The function to use disposable object and return result</param>
/// <returns>TResult from specified action function</returns>
public static TResult Using<TDisposable, TResult>(Func<TDisposable> factory, Func<TDisposable, TResult> action)
where TDisposable : IDisposable
{
using (var disposable = factory())
{
return action(disposable);
}
}
}