-
Notifications
You must be signed in to change notification settings - Fork 418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CopyTo
operator
#964
Add CopyTo
operator
#964
Conversation
Codecov Report
@@ Coverage Diff @@
## master #964 +/- ##
==========================================
- Coverage 92.52% 92.51% -0.02%
==========================================
Files 113 114 +1
Lines 3440 3498 +58
Branches 1024 1040 +16
==========================================
+ Hits 3183 3236 +53
- Misses 192 197 +5
Partials 65 65
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
{ | ||
return CopyTo(source, array, index); | ||
} | ||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡You don't need the else statement here, because there is no more instruction after it.
var i = index; | ||
foreach (var el in source) | ||
{ | ||
if (i < list.Count) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Sometimes you use { } and sometimes you don't use it in if statement. Adopt a single code style.
This PR adds a
CopyTo
operator, which operates similar toArray.CopyTo
orList.CopyTo
. This is useful for copying data from a stream to a pre-existing array orIList<>
, such as an array rented from anArrayPool<>
.To match semantics with existing methods, this method will throw an exception if there is not enough room in the specified destination for the input data to be stored. If the consumer does not care about excess data, they can apply the
Take
operator before callingCopyTo
to avoid the exception.One difference between this operator and the collection methods is that this version returns an
int
. With the collection methods, it is easy to know pre or post how much data should be copied, because theCount
/Length
property is available. ForIEnumerable<>
this information is not as readily available, soCopyTo
returns how many elements were stored in the destination.Fixes #963