-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
API Proposal: Range.Contains(Index) #417
Comments
How would an API like that work for something like:
where it doesn't have enough information to produce an answer? Where/how would you use this method? |
СпасибоОтправлено с моего телефона Huawei
|
The intended usage is to do a bound check using (1..3).Contains(^0) => false
(1..3).Contains(^1) => true
(1..3).Contains(^2) => true
(1..3).Contains(^3) => false
(1..3).Contains(^4) => false |
This is not how range and index work. |
Right. That's why I was asking how you expect such an API to be used. I'd expect the intended usage to be in the implementation of other types, and in such types it is likely going to be wrong. |
This proposal is for "is the given index within the range object", and not for "is the given index within the collection object". I understand that thus far,
I am using it to validate values read in web API: var index = ReadIndex();
var range = ReadRange();
// validate
if (!RangeContains(range, index))
{
return Error("index was out of range");
}
.. |
Thanks. I think that should not be part of Range. It's arguably changing the expected meaning of Index, and I expect it's likely to do more harm (from being misused) than good. |
Given a range
in order to check if a number
X
is within this range, one needs to remember thatstart
of the range is inclusive andend
is exclusive. Also, theend
value can be greater thanstart
. It would be helpful ifContains(Index)
is added either as a first-class or an extension method in Range class:Inspired by:
The text was updated successfully, but these errors were encountered: