Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Chrtran in .NET
Message
De
11/11/2008 07:06:40
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01360482
Message ID:
01361040
Vues:
47
This message has been marked as a message which has helped to the initial question of the thread.
>Thanks, Greg. I'll review the code this weekend.

Nadya,

In case you're intrested - I've added null support for the generic Between()
/ Generic
namespace GregoryAdam.Base.ExtensionMethods
{

	public static partial class ExtensionMethods
	{
		#region Between
		/// <summary>
		/// tells whether s between s1 and s2
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="s"></param>
		/// <param name="s1"></param>
		/// <param name="s2"></param>
		/// <returns>true or false</returns>
		//
		// IComparable<T>
		//	- By definition, any object compares greater than a null reference 
		//	- two null references compare equal to each other
		public static bool Between<T>(this T s, T s1, T s2) where T : IComparable<T>
		{
			if (s != null)
				return (s.CompareTo(s1) >= 0) && (s.CompareTo(s2) <= 0);

			// s == null here

			/*
			 *		s1		s		s2		s1<=s	s<=s2	final
			 *		!=null	null	!null	false	true	false
			 *		null	null	!null	true	true	true	*
			 *		!null	null	null	false	true	false
			 *		null	null	null	true	true	true	*
			 */
			/*
			 * conclusion
			 * It does not matter whether s2 is null or not
			 *		s < non-null-s2
			 *		s == null-s2
			 *		hence always:	s <= s2
			 *		
			 * the only thing that matters is s1
			 *		non_null_s1 < s
			 *		null_s1 == s
			 */
			return (s1 == null);
		}
#if false
		// 3 times slower
		public static bool Between(this IComparable s, IComparable s1, IComparable s2)
		{
			return (s.CompareTo(s1) >= 0) && (s.CompareTo(s2) <= 0);
		}
#endif
		#endregion
		//--------------------------------------------------------------------------------

		//--------------------------------------------------------------------------------

		#region InList
		// no problems with null references
		public static bool InList<T>(this T s, params T[] list) where T : IEquatable<T>
		{
			return list.Contains(s);
		}



		#endregion
		//--------------------------------------------------------------------------------

	}
}
Gregory
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform