Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C# 배열 끝에서 지정된 수의 요소를 반환하는 프로그램

<시간/>

TakeLast() 메서드를 사용하여 배열 끝에서 요소를 반환합니다.

먼저 배열을 선언하고 초기화합시다.

int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};

이제 마지막 세 가지 요소를 가져오겠습니다.

IEnumerable<int> units = prod.AsQueryable().TakeLast(3);

전체 코드를 살펴보겠습니다.

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};
      // value of last three products
      IEnumerable<int> units = prod.AsQueryable().TakeLast(3);
      foreach (int res in units) {
         Console.WriteLine(res);
      }
   }
}

출력

698
765
789