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.
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."
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.
a/ c is an abbreviation for air-conditioning. Keep your windows up and the a/c on high.
I found it best to roll my own. Some people use Tuple
s or Point
s, 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 Double
s, 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); } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With