目錄
一、關(guān)于Enumerable.Range(Int32, Int32) 方法
1.定義
2.Enumerable.Range()用于數(shù)學(xué)計(jì)算的操作方法
3.實(shí)例1:顯示整型數(shù)1~9的平方
4.實(shí)例2:顯示整型數(shù)0~9
5.實(shí)例3:Enumerable.Range()vs for循環(huán)?
(1)使用Enumerable.Range()
(2)使用for循環(huán)
(3)再用Enumerable.Range()
(4)再用Enumerable.Range()
一、關(guān)于Enumerable.Range(Int32, Int32) 方法
1.定義
????????命名空間:
????????System.Linq
????????程序集:
????????System.Linq.dll
????????在指定的范圍內(nèi)產(chǎn)生一系列整型數(shù)。
public static IEnumerable<int> Range (int start, int count);
參數(shù)
start Int32
序列整型數(shù)的起始位置
count Int32
序列中要產(chǎn)生的整型數(shù)的數(shù)量
返回值
IEnumerable<Int32>
指定范圍內(nèi)的一系列整型數(shù)
Exceptions
ArgumentOutOfRangeException
count >= 0
-or-
(start+count-1) >= Int32.MaxValue.
2.Enumerable.Range()用于數(shù)學(xué)計(jì)算的操作方法
????????先定義一個臨時變量用于裝載數(shù)據(jù)集,比如IEnumerable<int> squares,再從索引位置1開始枚舉產(chǎn)生10個整型數(shù),并定義臨時變量x通過select方法指向所產(chǎn)生的整型數(shù),計(jì)算x*x,Enumerable.Range(1, 10).Select(x => x * x)。文章來源:http://www.zghlxwxcb.cn/news/detail-807070.html
IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x);
3.實(shí)例1:顯示整型數(shù)1~9的平方
// 從1開始產(chǎn)生10個序列整型數(shù)并計(jì)算其平方;
// 遍歷輸出
namespace ConsoleApp11
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x);
foreach (int num in squares)
{
Console.WriteLine(num);
}
}
}
}
/*
運(yùn)行結(jié)果:
1
4
9
16
25
36
49
64
81
100
*/
4.實(shí)例2:顯示整型數(shù)0~9
//使用Enumerable.Range 打印數(shù)字0到9
namespace ConsoleApp12
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
var collection = Enumerable.Range(0, 10);
foreach (var element in collection)
{
Console.WriteLine(element);
}
Console.ReadLine();
}
}
}
//運(yùn)行結(jié)果:
/*
0
1
2
3
4
5
6
7
8
9
*/
5.實(shí)例3:Enumerable.Range()vs for循環(huán)?
(1)使用Enumerable.Range()
// Write the numbers 1 thru 7
namespace ConsoleApp13
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
foreach (int index in Enumerable.Range(1, 7))
{
Console.WriteLine(index);
}
}
}
}
//運(yùn)行結(jié)果:
/*
1
2
3
4
5
6
7
*/
(2)使用for循環(huán)
// Write the numbers 1 thru 7
namespace ConsoleApp14
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
for (int index = 1; index <= 7; index++)
{
Console.WriteLine(index);
}
}
}
}
//運(yùn)行結(jié)果:
/*
1
2
3
4
5
6
7
*/
(3)再用Enumerable.Range()
//從索引2開始的7個數(shù)
namespace ConsoleApp15
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
foreach (var index in Enumerable.Range(2, 7))
{
Console.WriteLine(index);
}
}
}
}
//運(yùn)行結(jié)果:
/*
2
3
4
5
6
7
8
*/
(4)再用Enumerable.Range()
//Enumerable.Range()
namespace ConsoleApp16
{
class Program
{
private static void Main()
{
Enumerable.Range(1, 7).ToList().ForEach(i => Console.WriteLine(2*i+1));
}
}
}
//運(yùn)行結(jié)果:
/*
3
5
7
9
11
13
15
*/
?文章來源地址http://www.zghlxwxcb.cn/news/detail-807070.html
到了這里,關(guān)于C#中Enumerable.Range(Int32, Int32) 方法用于計(jì)算的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!