본문 바로가기
C#/C# 강의 정리

C# 매개 변수 ref, out, method overloading

by 민트코코넛 2020. 12. 27.
728x90
반응형

◈ parameter

- 일반 변수 형식인 경우 : 변수 지정, 함수 내에서 변수에 대입될 값 지정.  (원본)

- 참조 + 일반 형식인 경우 : 외부 변수를 참조형식으로 가져온다.  (복사의 개념, 사본)

 

◈ void 함수 : 특정한 리턴값이 없는 함수의 형태

class Method
{
	public void Method_A()
	{
	}
}

 

§ 출력 전용 매개 변수 ref §

ref : Reference의 약어

- 참조에 의한 전달방식

  조건 1. 참조가 되기 전에 변수의 초기화 필수

  조건 2. 함수 내에서 값을 설정할 필요는 없다.

using System;

// 출력 전용 매개 변수. p198. ref, out 차이점
namespace MyNamespace
{
    class MyClass
    {   
        static void Divide(int a, int b, ref int quotient, ref int remainder)
        {
         // int형으로 a, b 데이터를 복사해서 가져오고,
         // quotient, remainder 라는 자료를 참조형으로 들고온다.
            quotient = a / b;   // 나누기
            // ↑↓ ref로 지정하여, 변수 a, b 를 계산 
            remainder = a % b;  // 몫
        }
        static void Main(string[] args)
        {
            int a = 20; // 외부 함수로 호출당하는 자료
            int b = 3;  // 외부 함수로 호출당하는 자료
            int c = 0;  // 참조할 변수를 초기화하여 호출 
            int d = 0;  // 참조할 변수를 미리 초기화하여 호출 
            Divide(a, b, ref c, ref d);
            Console.WriteLine("quotient : {0}, remainder : {1}", c, d);
        }
    }
}

§ 출력 매개 변수 out §

out

  조건 1. main 함수 내에서 변수의 초기화가 필요 없음.

  조건 2. 외부 함수 내에서 값을 반드시 설정하여 출력할 것

class Program
{
    static void Divide(int a, int b, out int quotient, out int remainder)
    {
    	quotient = a / b;   // 나누기
        // ↑↓ ref로 지정하여, 변수 a, b 를 계산 
        remainder = a % b;  // 몫
    }
    static void Main(string[] args)
    {
    	int a = 20; // 외부 함수로 호출당하는 자료
    	int b = 3;  // 외부 함수로 호출당하는 자료
    
    	Divide(a, b, out int c, out int d);
        Console.WriteLine("quotient : {0}, remainder : {1}", c, d);
    }
}

§ 가변(길이) 매개 변수 §

< 이것이 C#이다 > 교재 p204 ~ p205

- 형식은 같지만, 매개변수의 개수가 유연하게 달라질 수 있는 경우에 사용한다.

- 이때의 Keyword : paeams

class MyClass
{
    static int Sum(params int[] args)
    // main함수에서 파라미터로 int형의 자료를 배열로 받아서 가져온다.
    {
    	Console.WriteLine("Summing...");
        int sum = 0;
        for (int i = 0; i < args.Length; i++)
        {
            if (i > 0)
            {
            	Console.WriteLine(" , ");
            }
            Console.WriteLine(args[i]);
            sum += args[i];
        }
        Console.WriteLine();
        return sum;
    }
    static void Main(string[] args)
    {
    	int sum = Sum(3, 4, 5, 6, 7, 8, 9, 10, 55);
        Console.WriteLine($"Sum : {sum} \n");
    }
}

§ 명명된 매개 변수 §

< 이것이 C#이다 > 교재 p206 ~ p208

- string, char

  1. 단순한 값 입력

  2. 변수의 이름을 설정하여 값 입력

  3. 변수의 순서를 다르게 하거나, 이름을 제외하고 사용해도 출력은 동일함.

class MyClass
{
    static void PrintProfile(string name, string phone)
    {
    	Console.WriteLine("{0}, {1}", name, phone);
    }
    static void Main(string[] args)
    {
    	PrintProfile("이글", "123 - 4567");
    	// ▲ 단순하게 값 입력한 것
    	PrintProfile(name: "벨져", phone: "123-4568");
        // ▲ 변수의 이름을 설정하고 값 입력
        PrintProfile(phone: "123-4569", name: "다이무스");
        // ▲ 변수의 순서를 다르게 설정해도 가능하다는 의미
        PrintProfile(name : "자네트", "123-4560");
        // ▲ 변수의 이름을 제외하고 사용해도 가능하다
    }
}

§ 선택적 매개 변수 §

< 이것이 C#이다 > 교재 p208 ~ p211

- 하나의 매개변수에 디폴트 값을 설정하여, 호출할 때 편리하게 사용하는 기능적 변수

- 유용하고, 자주 사용되는 기능

class MyClass
{
    static void PrintProfile(string name, string phone = "111 - 2222")
    {
    	Console.WriteLine("{0}, {1}", name, phone);
    }
    static void Main(string[] args)
    {
    PrintProfile("이글");
    }
}

§ Method OverLoading §

< 이것이 C#이다 > 교재 p203

- 하나의 함수의 이름에 여러 종류의 기능을 구현하고,

  매개변수의 개수와 형식을 분석하여, 호출할 때, 해당 함수를 자동으로 결정하도록 구성하는 것

class MyClass
{
    static int Plus (int a, int b)
    {
    	Console.WriteLine("Calling int Plus(int, int)...");
    	return a + b;
    }
    static int Plus(int a, int b, int c)
    {
    	Console.WriteLine("Calling int Plus(int, int, int)...");
        return a + b + c;
    }
    static double Plus(double a, double b)
    {
    	Console.WriteLine("Calling douvle Plus(double a, double b)...");
        return a + b;
    }
    static  double Plus(int a, double b)
    {
    	Console.WriteLine("Calling double Plus(int, double...");
    	return a + b;
    }
    static void Main(string[] args)
    {
    	Console.WriteLine(Plus(1,2));           //  3
        Console.WriteLine(Plus(1,2,3));         //  6  
        Console.WriteLine(Plus(1.5f, 2.5));     //  4
        // float형을 호출할때는, 숫자 뒤에 f를 붙여줄 것.
        Console.WriteLine(Plus(1 , 2.5));       //  3.5
        // Console.WriteLine(Plus(1, 1, 2.5));  // 에러 원인 : 찾아갈만한 적정 메서드가 없음.
    }
}

 

728x90

'C# > C# 강의 정리' 카테고리의 다른 글

C# yield문  (0) 2020.12.29
C# 열거형 (Enum)  (0) 2020.12.29
C# 제어문  (0) 2020.12.27
C# 배열  (0) 2020.12.27
C# 함수  (0) 2020.12.27