반응형


named parameter


기초 문법이긴 한데 C++ 프로그래머에게는 낯설 수 있는 구문인 named parameter


별건 없고, 단지 좀 더 편의를 제공해 주는 기능 정도입니다


인자 값을넘겨 줄 때 변수명:값 형태로 넘겨 줄 수 있고 순서를 바꿔도 name 지정이기 때문에


넘어가는 것은 이름에 매칭되어 넘어갑니다



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
 
namespace Test
{
    public class Program
    {
 
        public void call(int first, int second)
        {
            Console.WriteLine(first+second);
        }
 
 
        static void Main()
        {
            int dd = 30;
            Program pi = new Program();
            pi.call(second: dd, first: 20);               //50
            pi.call(first: 20, second:2);                 //22
 
        }
 
    }
}
 







optional parameter 


는 C++ 에서 함수의 매개변수에 기본 값을 넣는 것과 동일하다고 생각하면 됩니다


가장 뒤의 매개변수 부터 채워져야 한다는 규칙을 갖고 있습니다





optional parameter 의 경우 오버로딩 함수들 중에서 어떤 함수로 호출될지를 변수 이름으로 지정할 수도 있습니다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
 
namespace Test
{
    public class Program
    {
 
        public void call(int third)
        {
            Console.WriteLine(third);
        }
 
 
        public void call(int first, int second=30)
        {
            Console.WriteLine(first+second);
        }
 
        static void Main()
        {
            int dd = 30;
            Program pi = new Program();
            pi.call(second: dd, first: 20);                //50
            pi.call(first: 20, second:30);                 //22
 
 
            pi.call(30);            //public void call(int third) 이 호출 됨
 
            pi.call(first:30);      //public void call(int first, int second=30) 이 호출됨
 
            pi.call(third: 30);     //public void call(int third)   이 호출됨
 
        }
    }
}
 

cs




위 처럼 변수명:값 으로 오버로딩 함수중 하나를 택할 수 있지만 만약



가장 상단의 call 함수를 아래 처럼 변경한다면


        public void call(int first)

        {

            Console.WriteLine(first);

        }




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
 
namespace Test
{
    public class Program
    {
 
 
        public void call(int first)
        {
            Console.WriteLine(first);
        }
 
 
        public void call(int first, int second=30)
        {
            Console.WriteLine(first+second);
        }
 
 
        static void Main()
        {
            int dd = 30;
            Program pi = new Program();
            pi.call(second: dd, first: 20);                //50
            pi.call(first: 20, second:30);                 //22
 
 
            pi.call(30);                        //public void call(int third) 이 호출 됨
 
            pi.call(first: 30);                 //public void call(int first)   이 호출됨!!!!!!!!
 
            pi.call(first: 30, second:70);     //public void call(int first, int second = 30)   이 호출됨
 
pi.call( 30, second: 70); //public void call(int first, int second = 30) 이 호출됨

//pi.call(first: 30, 70); //7.2 버전부터..
 
        }
 
    }
}
 

cs


반응형

+ Recent posts