Does c# have a function that returns a boolean for expression : if(value.inRange(-1.0,1.0)){}
?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Description
I suggest you use a extension method.
You can create this extension method in a generic way (thx to digEmAll and CodeInChaos, who suggest that). Then you can use that on every object that implements
IComparable
.Solution
then you do this
Update for perfectionists
After an extensive discussion with @CodeInChaos who said
You can name the function
IsInOpenInterval
too.More Information
Use
Math.Abs(value) <= 1.0
for exampleNot very clear, but may be inverse:
Enumerable. Range(-1, 2).Any(x=>x==value)
.By the way didn't compile this, writing from mobile.
edit
here I'm using Enumerable.Range
edit1
if we are talking about
floating point
numbers, it's enough to create anextension method
which generates the sequence of numbers by specifyingstart number
andoffset step
and after executeAny<T>
on resulting collection, like in aswer provided.