Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C# type for representing an integer Range?

Tags:

c#

I have a need to store an integer range. Is there an existing type for that in C# 4.0?

Of course, I could write my own class with int From and int To properties and build in proper logic to ensure that From <= To. But if a type already exists, I'd of course rather use that.

like image 963
James Maroney Avatar asked Mar 17 '11 17:03

James Maroney


People also ask

Why does AC have a slash?

Senior Member. You read it as "ei-cee" (no "slash" pronounced). In terms of distinguishing between "air conditioning" and "air conditioner," I can think of an example like "Today, I bought a new air conditioner" ("conditioning" not allowed). I personally would not say "Today, I bought a new AC."

Why is it called AC?

Air conditioning, often abbreviated as A/C or AC, is the process of removing heat from an enclosed space to achieve a more comfortable interior environment (sometimes referred to as 'comfort cooling') and in some cases also strictly controlling the humidity of internal air.

What do you mean by AC?

a/ c is an abbreviation for air-conditioning. Keep your windows up and the a/c on high.


2 Answers

I found it best to roll my own. Some people use Tuples or Points, but in the end you want your Range to be extensive and provide some handy methods that relate to a Range. It's also best if generic (what if you need a range of Doubles, or a range of some custom class?) For example:

/// <summary>The Range class.</summary> /// <typeparam name="T">Generic parameter.</typeparam> public class Range<T> where T : IComparable<T> {     /// <summary>Minimum value of the range.</summary>     public T Minimum { get; set; }      /// <summary>Maximum value of the range.</summary>     public T Maximum { get; set; }      /// <summary>Presents the Range in readable format.</summary>     /// <returns>String representation of the Range</returns>     public override string ToString()     {         return string.Format("[{0} - {1}]", this.Minimum, this.Maximum);     }      /// <summary>Determines if the range is valid.</summary>     /// <returns>True if range is valid, else false</returns>     public bool IsValid()     {         return this.Minimum.CompareTo(this.Maximum) <= 0;     }      /// <summary>Determines if the provided value is inside the range.</summary>     /// <param name="value">The value to test</param>     /// <returns>True if the value is inside Range, else false</returns>     public bool ContainsValue(T value)     {         return (this.Minimum.CompareTo(value) <= 0) && (value.CompareTo(this.Maximum) <= 0);     }      /// <summary>Determines if this Range is inside the bounds of another range.</summary>     /// <param name="Range">The parent range to test on</param>     /// <returns>True if range is inclusive, else false</returns>     public bool IsInsideRange(Range<T> range)     {         return this.IsValid() && range.IsValid() && range.ContainsValue(this.Minimum) && range.ContainsValue(this.Maximum);     }      /// <summary>Determines if another range is inside the bounds of this range.</summary>     /// <param name="Range">The child range to test</param>     /// <returns>True if range is inside, else false</returns>     public bool ContainsRange(Range<T> range)     {         return this.IsValid() && range.IsValid() && this.ContainsValue(range.Minimum) && this.ContainsValue(range.Maximum);     } } 
like image 53
drharris Avatar answered Sep 21 '22 07:09

drharris


Ranges and Indices are released with C#8.0 and .NET Core.

You are now able to do

string[] names = {     "Archimedes", "Pythagoras", "Euclid", "Socrates", "Plato" }; foreach (var name in names[1..4]) {     yield return name; } 

Check out https://blogs.msdn.microsoft.com/dotnet/2018/12/05/take-c-8-0-for-a-spin/ for more detail.

like image 34
Tom McDonough Avatar answered Sep 21 '22 07:09

Tom McDonough