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

C#의 DateTime.IsLeapYear() 메서드

<시간/>

C#의 DateTime.IsLeapYear() 메서드는 지정된 연도가 윤년인지 확인하는 데 사용됩니다. 반환 값은 부울이며 연도가 윤년이면 TRUE이고 그렇지 않으면 FALSE입니다.

구문

다음은 구문입니다 -

public static bool IsLeapYear (int y);

위의 y는 확인할 연도, 2010, 2016, 2019 등입니다.

이제 DateTime.IsLeapYear() 메서드를 구현하는 예를 살펴보겠습니다. -

using System;
public class Demo {
   public static void Main() {
      int year = 2019;
      Console.WriteLine("Year = "+year);
      if (DateTime.IsLeapYear(year)){
         Console.WriteLine("Leap Year!");
      } else {
         Console.WriteLine("Not a Leap Year!");
      }
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

Year = 2019
Not a Leap Year!

이제 DateTime.IsLeapYear() 메서드를 구현하는 또 다른 예를 살펴보겠습니다. 여기에 범위를 벗어난 연도를 추가합니다 -

using System;
public class Demo {
   public static void Main() {
      int year = 101910;
      Console.WriteLine("Year = "+year);
      if (DateTime.IsLeapYear(year)){
         Console.WriteLine("Leap Year!");
      } else {
         Console.WriteLine("Not a Leap Year!");
      }
   }
}

출력

그러면 다음과 같은 출력이 생성됩니다. 즉, 오류가 생성됩니다. 스택 추적은 아래와 같은 오류를 인쇄합니다 -

Year = 101910
Run-time exception (line 11): Year must be between 1 and 9999.
Parameter name: year
Stack Trace:
[System.ArgumentOutOfRangeException: Year must be between 1 and 9999.
Parameter name: year]
at System.DateTime.IsLeapYear(Int32 year) at Demo.Main() :line 11