מדי פעם אנחנו משתמשים בICollection ולא בטיפוס ספציפי (כמו List)
שמתי לב שאין אף ממשק עם פונקציה של AddRange. אפשר לפתור את הבעיה ככה:
ניצור פשוט Extension Method כזה:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14
 | public static void AddRange<T>(this ICollection<T> collection,                                IEnumerable<T> range) {     if (collection == null)     {         throw new ArgumentNullException             ("collection", "Received null");     }     foreach (T current in range)     {         collection.Add(current);     } }
 | 
הבעיה היא שמבט חטוף בReflector מראה שבList<T> יש מימוש יותר מסובך מהמימוש הנאיבי, כנראה טיפה יותר טוב באיזושהי צורה. לכן נוסיף טיפול מיוחד במקרה זה:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 | public static void AddRange<T>(this ICollection<T> collection,                                IEnumerable<T> range) {     if (collection == null)     {         throw new ArgumentNullException             ("collection",              "Received null");     }     List<T> list = collection as List<T>;     if (list != null)     {         list.AddRange(range);     }     else     {         foreach (T current in range)         {             collection.Add(current);         }     } }
 | 
כעת נוכל לכתוב קוד כזה בכיף:
| 1 2 3 4 5 6 7 8 9 10
 | ICollection<string> favoriteSongs =     new Collection<string>(); favoriteSongs.AddRange(new[]                            {                                "Shine on you crazy diamond",                                "Wish you were here",                                "Another brick in the wall",                                "Hey you"                            });
 | 
קצת מזכיר את הכתיבה של Collection Initializer (ראו גם טיפ מספר 18), אלא שאפשר להשתמש בזה גם לא בConstructor, ולמעשה אפשר לעשות שילובים יפים כאלה:
| 1 2 3 4 5 6 7
 | favoriteSongs.AddRange(new List<string>                            {                                "Shine on you crazy diamond",                                "Wish you were here",                                "Another brick in the wall",                                "Hey you"                            });
 | 
יתרה מזו, בהינתן שAddRange של List<T> יותר טוב מזה שאנחנו כתבנו, אז הכתיבה הבאה עדיפה על הכתיבה עם Collection Initializer:
| 1 2 3 4 5 6 7 8 9 10
 | ICollection<string> favoriteSongs =     new List<string>(); favoriteSongs.AddRange(new []                            {                                "Shine on you crazy diamond",                                "Wish you were here",                                "Another brick in the wall",                                "Hey you"                            });
 | 
עדיפה מזו:
| 1 2 3 4 5 6 7 8
 | ICollection<string> favoriteSongs =     new List<string>         {             "Shine on you crazy diamond",             "Wish you were here",             "Another brick in the wall",             "Hey you"         };
 | 
מהסיבה ששימוש בCollection Initializer שקול לשימוש בפונקציה שאנחנו כתבנו.
יום מורחב טוב