본문 바로가기
프로그래밍/C# LINQ

C# Linq - Enumerable.Range()

by bantomak 2023. 12. 18.

Enumerable.Range(Int32, Int32) 메서드

public static System.Collections.Generic.IEnumerable<int> Range (int start, int count);

매개변수

start int32

시퀀스의 첫번째 정수값 (시작값)

count int32

생성할 순차적 정수의 개수

 

특정 시작값에서부터 순차적으로 정수를 생성하고 싶을때 사용한다.

 

정리하자면

Enumerable.Range(0, 5); 라고 한다면

 

0, 1, 2, 3, 4 

 

로 정수의 나열이 생성된다.

 

예제 코드

// Generate a sequence of integers from 1 to 10
// and then select their squares.
IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x);

foreach (int num in squares)
{
    Console.WriteLine(num);
}

/*
 This code produces the following output:

 1
 4
 9
 16
 25
 36
 49
 64
 81
 100
*/

 

함께 읽으면 좋은 글

 

C# Linq - Enumerable.Repeat 메서드

Enumerable.Repeat(TResult, Int32) 메서드 public static System.Collections.Generic.IEnumerable Repeat (TResult element, int count); 매개변수 element TResult 반복할 값 count int32 값을 반복할 횟수 특정 타입의 값을 특정 횟수 반

jettstream.tistory.com

 

출처

 

Enumerable.Range(Int32, Int32) 메서드 (System.Linq)

지정된 범위 내의 정수 시퀀스를 생성합니다.

learn.microsoft.com

'프로그래밍 > C# LINQ' 카테고리의 다른 글

C# Linq - Enumerable.Where()  (0) 2024.02.15
C# Linq - Enumerable.All()  (0) 2024.02.15
C# Linq - Enumerable.Aggregate()  (19) 2023.06.26
C# Linq - Enumerable.OrderBy()  (26) 2023.06.20
C# Select vs SelectMany  (22) 2023.06.19

댓글