C#中是否有一个类可以处理几个INT(范围为2个INT-1-10)
我对 C# 很陌生,我想知道是否有类或数据结构或处理以下要求的最佳方法... 我需要处理代表一系列数据的几个 int(例如 1 - 10 或 5-245),并且我需要一种方法来验证 Int 值是否包含在该范围内... 我相信在 C# 中,框架中内置了一个类来处理我的需求...... 我需要做的是验证 INT (例如 5)是否包含在值范围例如(1-10)中......
如果我发现没有一个类来处理它,我我正在考虑使用包含 2 个数字的结构,并创建我自己的 Contain 方法来测试 5 是否包含在范围 1-10 中)
I am quite new to C# and I was wondering if there is a Class or a data structure or the best way to handle the following requirement...
I need to handle a COUPLE of int that represent a range of data (eg. 1 - 10 or 5-245) and I need a method to verify if an Int value is contained in the range...
I believe that in C# there is a class built in the framework to handle my requirement...
what I need to do is to verify if an INT (eg. 5) is contained in the range of values Eg (1-10) ...
in the case that I should discover that there is not a class to handle it, I was thinking to go with a Struct that contain the 2 numbers and make my own Contain method to test if 5 is contained in the range 1-10)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这实际上是一个好主意,因为 BCL 中没有适合您的场景的内置类。
That's actually a great idea as there's no built-in class for your scenario in the BCL.
您正在寻找范围类型; .Net 框架不包括这一点。
您应该按照您的建议创建一个不可变的 (!)
Int32Range
结构。您可能需要实现
IEnumerable
以允许用户轻松循环范围内的数字。您需要决定每个界限是包含还是排除。
[Start, End)
可能是最明显的选择。无论您选择什么,都应该在 XML 注释中清楚地记录它。
You're looking for a range type; the .Net framework does not include one.
You should make an immutable (!)
Int32Range
struct, as you suggested.You may want to implement
IEnumerable<int>
to allow users to easily loop through the numbers in the range.You need to decide whether each bound should be inclusive or exclusive.
[Start, End)
is probably the most obvious choice.Whatever you choose, you should document it clearly in the XML comments.
没有任何东西可以完全满足您的要求。
假设我理解正确,那么该类编写起来非常简单。
Tuple
可以帮助您完成部分任务,但您必须添加扩展方法才能获得额外的行为。缺点是下限和上限是隐式的Item1
和Item2
,这可能令人困惑。Nothing exists that meets your requirements exactly.
Assuming I understood you correctly, the class is pretty simple to write.
A
Tuple<int,int>
would get you part of the way but you'd have to add an extension method to get the extra behavior. The downside is that the lower- and upper-bounds are implicitlyItem1
andItem2
which could be confusing.如果您想避免创建新类型,您可以创建一个扩展:
然后您可以执行以下操作:
You could create an extension if you want to avoid making a new type:
Then you could do something like:
我认为你可以用数组来做到这一点。
一些很好的例子和解释可以在这里找到:
http://www.dotnetperls.com/int-array
i think you can do that with an array.
some nice examples and explanation can be found here:
http://www.dotnetperls.com/int-array
AFAIK 中没有内置任何内容,但是(取决于范围的大小) Enumerable.Range 可以工作(但不是最佳的,因为您实际上存储范围中的每个值,而不仅仅是端点)。不过,它确实允许您使用 LINQ 方法(包括 Enumerable.Contains)——这可能会派上用场。
就我个人而言,我可能会继续编写该类,因为它相当简单(如果您想对其执行 LINQ,您始终可以通过 Enumerable.Range 公开
IEnumerable
迭代器)Nothing built in AFAIK, but (depending on the size of the range) an Enumerable.Range would work (but be less than optimal, as you're really storing every value in the range, not just the endpoints). It does allow you to use the LINQ methods (including Enumerable.Contains), though - which may come in handy.
Personally, I'd probably go ahead and write the class, since it's fairly simple (and you can always expose an
IEnumerable<int>
iterator via Enumerable.Range if you want to do LINQ over it)