Guest User

Untitled

a guest
Nov 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. List<AttendeeInfo> attendees = new List<AttendeeInfo>();
  2. foreach ...
  3. // Error: "There are too many target users in the email address array"
  4. // for more than 100 attendees. So take the first 100 attendees only.
  5. if(attendees.Count > 100) attendees = attendees.GetRange(0,100);
  6. // or
  7. if(attendees.Count > 100) attendees = attendees.Take(100).ToList();
  8.  
  9. var list = Enumerable.Range(0, 1000).ToList();
  10.  
  11. var stopwatch = new Stopwatch();
  12.  
  13. stopwatch.Start();
  14.  
  15. for(var i=0; i<1000000; i++)
  16. {
  17. var c = list.GetRange(0, 100);
  18. }
  19.  
  20. Console.WriteLine(stopwatch.Elapsed);
  21.  
  22. stopwatch.Restart();
  23.  
  24. for (var i = 0; i < 1000000; i++)
  25. {
  26. var c = list.Take(100).ToList();
  27. }
  28.  
  29. Console.WriteLine(stopwatch.Elapsed);
  30.  
  31. public List<T> GetRange(int index, int count)
  32. {
  33. if (index < 0)
  34. {
  35. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  36. }
  37. if (count < 0)
  38. {
  39. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  40. }
  41. if ((this._size - index) < count)
  42. {
  43. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  44. }
  45. List<T> list = new List<T>(count);
  46. Array.Copy(this._items, index, list._items, 0, count); // Implemented natively
  47. list._size = count;
  48. return list;
  49. }
  50.  
  51. public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, int count)
  52. {
  53. if (source == null)
  54. {
  55. throw Error.ArgumentNull("source");
  56. }
  57. return TakeIterator<TSource>(source, count);
  58. }
  59.  
  60. private static IEnumerable<TSource> TakeIterator<TSource>(IEnumerable<TSource> source, int count)
  61. {
  62. if (count > 0)
  63. {
  64. foreach (TSource iteratorVariable0 in source)
  65. {
  66. yield return iteratorVariable0;
  67. if (--count == 0)
  68. {
  69. break;
  70. }
  71. }
  72. }
  73. }
  74.  
  75. public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
  76. {
  77. if (source == null)
  78. {
  79. throw Error.ArgumentNull("source");
  80. }
  81. return new List<TSource>(source);
  82. }
  83.  
  84. public List(IEnumerable<T> collection)
  85. {
  86. if (collection == null)
  87. {
  88. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
  89. }
  90. ICollection<T> is2 = collection as ICollection<T>;
  91. if (is2 != null)
  92. {
  93. int count = is2.Count;
  94. if (count == 0)
  95. {
  96. this._items = List<T>._emptyArray;
  97. }
  98. else
  99. {
  100. this._items = new T[count];
  101. is2.CopyTo(this._items, 0);
  102. this._size = count;
  103. }
  104. }
  105. else
  106. {
  107. this._size = 0;
  108. this._items = List<T>._emptyArray;
  109. using (IEnumerator<T> enumerator = collection.GetEnumerator())
  110. {
  111. while (enumerator.MoveNext())
  112. {
  113. this.Add(enumerator.Current);
  114. }
  115. }
  116. }
  117. }
Add Comment
Please, Sign In to add comment