Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Foreach vs AddRange
Message
De
12/08/2009 01:35:03
 
 
À
11/08/2009 13:47:23
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01417359
Message ID:
01417463
Vues:
48
Adding an extension method ( say AddCollection) is a bad idea since it accepts an argument of IEnumerable(T).

using items.Count() is slow - I guess it uses a foreach loop to count
// don't do this at home

public static void AddCollection<T>(this List<T> list, IEnumerable<T> items)
		{

			if (items.Count() <= Tuning.AddRangeMinimumItems) // 128
			{
				foreach (T item in items)
					list.Add(item);
			}
			else
			{
				list.AddRange(items);
			}
		}
An extension method that takes a whole array is a better idea - since array.Length is faster
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform