Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Foreach vs AddRange
Message
From
13/08/2009 08:18:33
 
 
To
13/08/2009 07:59:30
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01417359
Message ID:
01417827
Views:
37
>>>>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
>>>
>>>And probably I wouldn't use such an extesnion when it already exists (Concat):
>>>
>>>       static void UseConcat()
>>>        {
>>>            int[] buf = new int[Size];
>>>            List<int> list = new List<int>();
>>>            for (int i = nTimes; --i != 0; )
>>>            {
>>>                list.Concat(buf);
>>>                list.Clear();
>>>            }
>>>       }
>>>
>>>Cetin
>>
>>
>>I thought so as well - for a couple of minutes
>>
>>They are both extension methods - but they are not doing the same, ie one works on the list whereas the other returns another Enumerator
>
>Yes and I can't see why I wouldn't want that. That is exactly what I would like it to be (but that is me:).
>Cetin

Not sure what you mean here, Cetin
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform