메서드

Fruit·2023년 3월 28일

✨ Hello C#!

목록 보기
17/34
post-thumbnail

🌸 메서드

  • 클래스 안에서 선언된다.
using System;

namespace Method
{
    class MainApp
    {
    	// Plus() 메서드
        public static int Plus (int a, int b)       // 매개변수: a, b
        {
            return a + b;
        }
        
        public static void Main ()
        {
            int result = MainApp.Plus(3, 4);        // 인수: 3, 4

            Console.WriteLine(result);
        }
    }
}

[실행 결과]
7

✔️ static

  • 클래스의 인스턴스가 아닌 클래스 자체에 소속되도록 지정하는 한정자이다.
  • 위의 경우 MainApp의 인스턴스를 만들지 않고 Plus() 메서드를 호출할 수 있다.


▪ 참고: Hello Fruit - 메서드 오버로딩 등
▪ 참고: Hello Fruit - 명명된 인수 등
▪ 참고: Hello Fruit - static 필드, 메서드

profile
🌼인생 참 🌻꽃🌻 같다🌼

0개의 댓글