|
using System;
namespace DanielVaughan.Logging
{
/// <summary>
/// Utility class for validating method parameters.
/// </summary>
public static class ParameterValidator
{
/// <summary>
/// Ensures the specified value is not null.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="value">The value to test.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <returns>The specified value.</returns>
/// <exception cref="ArgumentNullException">Occurs if the specified value
/// is <code>null</code>.</exception>
public static T EnsureNotNull<T>(T value, string parameterName) where T : class
{
if (value == null)
{
throw new ArgumentNullException(parameterName);
}
return value;
}
/// <summary>
/// Ensures the specified value is not <code>null</code> or empty (a zero length string).
/// </summary>
/// <param name="value">The value to test.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <returns>The specified value.</returns>
/// <exception cref="ArgumentNullException">Occurs if the specified value
/// is <code>null</code> or empty (a zero length string).</exception>
public static string EnsureNotNullOrEmpty(string value, string parameterName)
{
if (value == null)
{
throw new ArgumentNullException(parameterName);
}
if (value.Length < 1)
{
throw new ArgumentException("Parameter should not be an empty string.", parameterName);
}
return value;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.
Previously Daniel was a nine-time Microsoft MVP and co-founder of
Outcoder, a Swiss software and consulting company.
Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.
Daniel is the developer behind several acclaimed mobile apps including
Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably
Codon.
Would you like Daniel to bring value to your organisation?
Please contact
Blog |
Twitter
Xamarin Experts
Windows 10 Experts