Skip to content

Instantly share code, notes, and snippets.

@khalidsalomao
Created February 17, 2013 00:23
Show Gist options
  • Save khalidsalomao/4969410 to your computer and use it in GitHub Desktop.
Save khalidsalomao/4969410 to your computer and use it in GitHub Desktop.
Some helpful extension methods
using System;
using System.Collections.Generic;
using System.Linq;
namespace Helpers
{
public static class CommonExtensions
{
public static IEnumerable<IEnumerable<T>> Batch<T> (this IEnumerable<T> collection, int batchSize)
{
List<T> nextbatch = new List<T> (batchSize);
foreach (T item in collection)
{
nextbatch.Add (item);
if (nextbatch.Count == batchSize)
{
yield return nextbatch;
nextbatch = new List<T> (batchSize);
}
}
if (nextbatch.Count > 0)
yield return nextbatch;
}
/// <summary>
/// Converts a string to the desirable type with default value an error handling.
/// </summary>
/// <param name="text">The string.</param>
/// <param name="raiseOnError">The raise on error.</param>
/// <returns></returns>
public static T ConvertTo<T> (this string text, bool raiseOnError = true)
{
return text.ConvertTo<T> (default (T), raiseOnError);
}
/// <summary>
/// Converts a string to the desirable type with default value an error handling.
/// </summary>
/// <param name="input">The string.</param>
/// <param name="defaultValue">The default value, if the conversion is not possible.</param>
/// <param name="raiseOnError">The raise on error.</param>
/// <returns></returns>
/// <remarks>
/// Acknowledgments:
/// http://csharp-extension.blogspot.com.br/2011/07/convert-type-extension.html
/// http://tiredblogger.wordpress.com/2008/04/15/convertto-extension-method/
/// </remarks>
public static T ConvertTo<T> (this object input, T defaultValue = default(T), bool raiseOnError = false)
{
if (input != null)
{
try
{
return (T)Convert.ChangeType (input, typeof (T));
}
catch (Exception ex)
{
if (raiseOnError)
throw ex;
}
}
return defaultValue;
}
/// <summary>
/// Converts a string to the desirable type with default value an error handling.
/// </summary>
/// <param name="input">The string.</param>
/// <param name="defaultValue">The default value, if the conversion is not possible</param>
/// <param name="format">The format provider.</param>
/// <param name="raiseOnError">The raise on error.</param>
/// <returns></returns>
/// Acknowledgments:
/// http://csharp-extension.blogspot.com.br/2011/07/convert-type-extension.html
/// http://tiredblogger.wordpress.com/2008/04/15/convertto-extension-method/
/// </remarks>
public static T ConvertTo<T> (this object input, T defaultValue, IFormatProvider format, bool raiseOnError = false)
{
if (input != null)
{
try
{
return (T)Convert.ChangeType (input, typeof (T), format);
}
catch (Exception ex)
{
if (raiseOnError)
throw ex;
}
}
return defaultValue;
}
/// <summary>
/// Throws an ArgumentNullException if the given data item is null.
/// </summary>
/// <param name="data">The item to check for nullity.</param>
/// <param name="name">The name to use when throwing an exception, if necessary</param>
/// <remarks>
/// Acknowledgments:
/// http://stackoverflow.com/questions/11522104/what-is-the-best-way-to-extend-null-check
/// </remarks>
public static void ThrowIfNull<T> (this T data, string name) where T : class
{
if (data == null)
{
throw new ArgumentNullException (typeof (T).Name + " " + name);
}
}
/// <summary>
/// Throws an ArgumentNullException if the given data item is null.
/// </summary>
/// <param name="data">The item to check for nullity.</param>
/// <param name="name">The name to use when throwing an exception, if necessary</param>
/// <param name="message">The message.</param>
/// <remarks>
/// Acknowledgments:
/// http://stackoverflow.com/questions/11522104/what-is-the-best-way-to-extend-null-check
/// </remarks>
public static void ThrowIfNull<T> (this T data, string name, string message) where T : class
{
if (data == null)
{
throw new ArgumentNullException (typeof (T).Name + " " + name, message + ". ");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment