ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C#] ref, ref Return, out, params, 그 외 매개 변수 사용 예제
    C# 2023. 6. 1. 19:43

    1. ref 사용 예시

    참조에 의한 매개 변수 전달

    /*
    출력
    x:3, y:4
    x:4, y:3
     */
    namespace SwapByRef
    {
        internal class Program
        {
            static void Swap(ref int a, ref int b)
            {
                int temp = b;
                b = a; 
                a = temp;
            }
    
            static void Main(string[] args)
            {
                int x = 3;
                int y = 4;
    
                Console.WriteLine($"x:{x}, y:{y}");
    
                Swap( ref x, ref y );
    
                Console.WriteLine($"x:{x}, y:{y}");
            }
        }
    }

    2. ref return 사용 예제

    /*
    출력
    Price :100
    Ref Local Price :100
    Normal Local Price :100
    Price :200
    Local Price :200
    Normal Local Price : 100
     */
    namespace RefReturn
    {
        class Product
        {
            private int price = 100;
    
            public ref int GetPrice()
            {
                return ref price;
            }
    
            public void PrintPrice()
            {
                Console.WriteLine($"Price :{price}");
            }
        }
    
        internal class Program
        {
            static void Main(string[] args)
            {
                Product carrot = new Product();
                ref int ref_local_price = ref carrot.GetPrice();
                int normal_local_price = carrot.GetPrice();
    
                carrot.PrintPrice(); // Price :100
                Console.WriteLine($"Ref Local Price :{ref_local_price}"); // Ref Local Price :100
                Console.WriteLine($"Normal Local Price :{normal_local_price}"); // Normal Local Price :100
    
                ref_local_price = 200;
    
                carrot.PrintPrice(); // Price :200
                Console.WriteLine($"Local Price :{ref_local_price}"); // Local Price :200
                Console.WriteLine($"Normal Local Price : {normal_local_price}"); // Normal Local Price : 100
            }
        }
    }

    3. 출력 전용 매개 변수 out 사용 예제

    /*
    실행
    a:20, b:3, a/b:6, a%b:2
     */
    namespace UsingOut
    {
        internal class Program
        {
            static void Divide (int a, int b, out int quotient, out int remainder)
            {
                quotient = 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($"a:{a}, b:{b}, a/b:{c}, a%b:{d}");
            }
        }
    }

    4. 가변길이 매개 변수 params 사용 예제

    /*
    실행
    Summing...3, 4, 5, 6, 7, 8, 9, 10
    Sum : 52
     */
    namespace UsingParams
    {
        internal class Program
        {
            static int Sum (params int[] args)
            {
                Console.Write("Summing...");
    
                int sum = 0;
    
                for(int i = 0; i < args.Length; i++)
                {
                    if (i > 0) Console.Write(", ");
    
                    Console.Write(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);
    
                Console.WriteLine($"Sum : {sum}");
            }
        }
    }

    5. 명명된 매개 변수 사용 예제

    /*
    실행 값
    Name:박찬호, Phone:010-123-1234
    Name:박지성, Phone:010-987-9876
    Name:박세리, Phone:010-222-2222
    Name:박상현, Phone:010-567-5678
     */
    namespace NamedParameter
    {
        internal class Program
        {
            static void PrintProfile(string name, string phone)
            {
                Console.WriteLine($"Name:{name}, Phone:{phone}");
            }
    
            static void Main(string[] args)
            {
                PrintProfile(name: "박찬호", phone: "010-123-1234");
                PrintProfile(phone: "010-987-9876", name:"박지성");
                PrintProfile("박세리", "010-222-2222");
                PrintProfile("박상현", phone: "010-567-5678");
            }
        }
    }

    6. 선택적 매개 변수 예제

    static void PrintProfile(string name, string phone = "")
            {
                Console.WriteLine($"Name :{name}, Phone :{phone}");
            }
    메소드의 매개 변수는 기본값을 가질 수 있음
    기본값을 가지는 매개 변수는 메소드를 호출할 때 데이터 할당을 생략할 수 있음
    /*
    실행 결과
    Name :태연, Phone :
    Name :윤아, Phone :010-123-1234
    Name :유리, Phone :
    Name :서현, Phone :010-789-7890
     */
    namespace OptionalParameter
    {
        internal class Program
        {
            static void PrintProfile(string name, string phone = "")
            {
                Console.WriteLine($"Name :{name}, Phone :{phone}");
            }
    
            static void Main(string[] args)
            {
                PrintProfile("태연");
                PrintProfile("윤아", "010-123-1234");
                PrintProfile(name:"유리");
                PrintProfile(name:"서현", phone:"010-789-7890");
            }
        }
    }

    7. 로컬 함수

    로컬 함수(Local Function)는 메소드 안에서 선언되고, 선언된 메소드 안에서만 사용되는 함수이다.
    로컬 함수는 자신이 소속한 메소드의 지역 변수를 사용할 수 있다.
    /*
    실행
    hello!
    good morning.
    this is c#.
     */
    namespace LocalFunction
    {
        internal class Program
        {
            static string ToLowerString (string input)
            {
                var arr = input.ToCharArray();
                for(int i = 0; i < arr.Length; i++)
                {
                    arr[i] = ToLowerChar(i); // 로컬 함수 호출
                }
    
                char ToLowerChar(int i) // 로컬 함수 선언
                { // 로컬 함수는 자신이 소속된 메소드의 지역 변수를 사용할 수 있습니다.
                    if (arr[i] < 65 || arr[i] > 90)
                        return arr[i];
                    else
                        return (char)(arr[i] + 32);
                }
    
                return new string(arr);
            }
    
            static void Main(string[] args)
            {
                Console.WriteLine(ToLowerString("Hello!"));
                Console.WriteLine(ToLowerString("Good Morning."));
                Console.WriteLine(ToLowerString("This is C#."));
            }
        }
    }

    댓글