C#교과서 마스터하기 26. 메서드와 매개 변수

min seung moon·2021년 7월 13일
0

C#

목록 보기
27/54

https://www.youtube.com/watch?v=7XtfWhT4hp0&list=PLO56HZSjrPTB4NxAsEP8HRk6YKBDLbp7m&index=60

1. 클래스에 메서드 정의

  • 클래스의 기능(동작)은 메서드로 표현

2. 메서드(Method)

  • 액세스 한정자(Accessor)
  • 반환값(Return Type)
    • 반환값은 모든 타입(any Type)이 될 수 있다
    • 반환 값 없는 경우에는 void
  • 메서드(함수)명(Function Name)
  • 매개변수(Parameters)

3. 시그니처 : 메서드명과 매개변수 리스트

  • 함수명과 매개변수 리스트
  • 메서드 오버로드의 구분점이 됨

4. 매개변수(파라미터) 전달 기법

  • 파라미터 전달의 3가지 방법

01. 값에 의한 전달(Pass by Value)

  • Call by Value
  • 파라미터 전달의 기본 기법
    • 파라미터 값이 복사됨
    • 변수는 메서드 안에서 바뀜
    • 메서드 밖에 있는 값에는 영향을 미치지 않음
    • 파라미터는 반드시 같거나 호환 가능해야 함

02. 참조에 의한 전달(Pass by Reference)

  • Call by Reference
  • 참조형 매개변수란?
    • 참조란 메모리 상의 위치(포인터)
  • 참조형 매개 변수 사용
    • 형식과 변숫값이 맞아야 함
    • 호출자에 의해서 원본 데이터에 영향
    • 메서드를 호출하기 전에 매개변수를 초기화해야 함

03. Output 매개변수(Output Parameters)

  • Output 매개변수
    • 매개 변수의 값이 메서드 안에서 밖으로 전달
  • Output 매개변수 사용
    • 기본적으로 ref 매개변수와 같다
      • 그렇지만 값이 메서드 안으로 전달되지는 않음
    • 메서드 선언과 호출 시 out 키워드 사용

04. 가변형 매개변수 : C#

  • params 키워드 사용
  • 값 전달
  • 한 번에 여러 개의 매개변수(배열형) 전달 가능

05. 선택적 매개변수(Optional Parmeter)

  • 메서드의 매개변수를 초기화 해줌으로서 초기화한 변수는 선택적으로 전달
public void PrintMessage(string messege, string prefix = "", string suffix ="")
{
	WriteLine($"{prefix}-{message}-{suffix}");
}

5. 프로젝트

01. 값에 의한 전달

  • Car.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace testProject
{
    class Car
    {
        public void Map(string title) => Console.WriteLine($"[2]{title}");
    }
}
  • Program.cs
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    
    class Program
    {
        
        static void Main(string[] args)
        {
            string title = "복사";
            WriteLine($"[1]{title}");
            Car car = new Car();
            car.Map(title);
            WriteLine($"[3]{title}");
        }

    }
}

02. 참조에 의한 값 전달

  • Car.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace testProject
{
    class Car
    {
        public void Map(ref string title)
        {
            title = "참조 변경";
            Console.WriteLine($"[2]{title}");
        }
    }
}
  • Program.cs
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    
    class Program
    {
        
        static void Main(string[] args)
        {
            string title = "복사";
            WriteLine($"[1]{title}");
            Car car = new Car();
            car.Map(ref title);
            WriteLine($"[3]{title}");
        }

    }
}


03. Output 매개변수

  • Car.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace testProject
{
    class Car
    {
        public void Map(out string title)
        {
            title = "참조 변경";
            Console.WriteLine($"[2]{title}");
        }
    }
}
  • Program.cs
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    
    class Program
    {
        
        static void Main(string[] args)
        {
            string title;
            
            Car car = new Car();
            car.Map(out title);
            WriteLine($"[3]{title}");
        }

    }
}

04. 가변형 매개변수

  • Car.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace testProject
{
    class Car
    {
        public void Map(params string[] titles)
        {
            foreach(string title in titles)
            {
                Console.WriteLine(title);
            }
        }
    }
}
  • Program.cs
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    
    class Program
    {
        
        static void Main(string[] args)
        {
            Car car = new Car();
            car.Map("유재석", "강호동");
        }

    }
}

05. 선택적 매개변수

  • Car.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace testProject
{
    class Car
    {
        public void Map(string title, string subTitle = "부제목")
        {
            Console.WriteLine($"{title}({subTitle})");
        }
    }
}
  • Program.cs
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace testProject
{
    
    class Program
    {
        
        static void Main(string[] args)
        {
            Car car = new Car();
            car.Map("C#");
            car.Map("C#", "C#교과서 마스터하기");
            car.Map(subTitle: "프로그래밍언어", title: "C#");
        }

    }
}

profile
아직까지는 코린이!

0개의 댓글