To find the range of all elements in an array in C#, you can use the Min
and Max
methods of the System.Linq
namespace. These methods return the minimum and maximum values in a sequence, respectively. You can apply them to the array to find the range of all elements as shown below:
main.cs153 chars8 lines
Output:
main.cs35 chars2 lines
Here, we first import the System.Linq
namespace and define an integer array arr
with some sample values. We then calculate the range by subtracting the minimum value from the maximum value of the array, and store it in the range
variable. Finally, we print the range to the console.
gistlibby LogSnag