C#/수업내용

0312 클래스+메서드

피주빈 2021. 3. 12. 10:55

out parameter

class Program
    {
        static void Main(string[] args)
        {
            int x;

            Multiplication(out x);

            Console.WriteLine("Variable Value: {0}", x);

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

        public static void Multiplication(out int a)

        {

            a = 10;

            a *= a;

        }
    }

 

ref parameter

class Program

    {

        static void Main(string[] args)

        {

            int x = 10;

            Console.WriteLine("Variable Value Before Calling the Method: {0}", x);

            Multiplication(ref x);

            Console.WriteLine("Variable Value After Calling the Method: {0}", x);

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

        public static void Multiplication(ref int a)

        {

            a *= a;

            Console.WriteLine("Variable Value Inside the Method: {0}", a);

        }

    }

 

params keyword = 배열 / 세트

int[] arr={1,2,3,4,5,6};

class Program
    {
        static void Main(string[] args)
        {
            ParamsMethod1(1, 2, 3, 4, 5, 6);
            ParamsMethod2(1, 2, 3, 4, 5, 6);
            ParamsMethod3(1, 2, "홍길동", "임꺽정", 10.24, new Unit());
        }

        public static void ParamsMethod1(int a, int b, int c, int d, int e, int f)
        {
            Console.WriteLine(a);
        }
        public static void ParamsMethod2(params int[] arr)
        {
            Console.WriteLine(arr[0]);
        }
        public static void ParamsMethod3(params object[] arr)
        {
            object obj = arr[5];
            Unit unit = (Unit)obj; //object를 호출하고 싶으면 언박싱 해줘야 함
        }
    }