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

IsDefault

Returns true if the object it is called upon is the default of its type. This will be null for referece types, zero for integer types, and a default-initialized struct for structs.

Source

/// <summary>
/// To include functions for validation on basic data types i.e. if value is default or null
/// </summary>    
public static class ObjectExtensions
{
	/// <summary>
	/// Checks to see if the object has null default value for basic types
	/// </summary>
	/// <typeparam name="T">Type of object being passed</typeparam>
	/// <param name="value">Object whose value needs to be checked</param>
	/// <returns>true if the value is null default. Otherwise returns false</returns>
	public static bool IsDefault<T>(this T value)
	{
		return (Equals(value, default(T)));
	}
}

Example

if (someObject.IsDefault())
{
   ...
}

Author: James Michael Hare (BlackRabbitCoder)

Submitted on: 14 okt. 2010

Language: C#

Type: System.Object

Views: 7677