Skip to content

Instantly share code, notes, and snippets.

@kcargile
Created September 14, 2012 19:29
Show Gist options
  • Save kcargile/3724170 to your computer and use it in GitHub Desktop.
Save kcargile/3724170 to your computer and use it in GitHub Desktop.
.NET Clones the current instance in a way that is safe even it if it null.
namespace Extensions
{
/// <summary>
/// Contains <see cref="object"/> extension methods.
/// </summary>
public static class ObjectExtensions
{
/// <summary>
/// Clones the current instance in a way that is safe even it if it null.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="param">The param.</param>
/// <returns>A deep copy of <b>param</b> or null.</returns>
public static T NullSafeClone<T>(this T param) where T : class, ICloneable
{
return null != param ? (T)param.Clone() : null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment