C#/이것이 C#이닀

16. κ°’, 참쑰에 μ˜ν•œ λ§€κ°œλ³€μˆ˜ 전달, ref와 out의 차이

Rainbow🌈Coder 2022. 2. 9. 18:26
728x90

값에 μ˜ν•œ 전달이 λ§€κ°œλ³€μˆ˜κ°€ λ³€μˆ˜λ‚˜ μƒμˆ˜λ‘œλΆ€ν„° 값을 λ³΅μ‚¬ν•˜λŠ” 것과 달리,

참쑰에 μ˜ν•œ 전달은 λ§€κ°œλ³€μˆ˜κ°€ λ©”μ†Œλ“œμ— λ„˜κ²¨μ§„ μ›λ³Έ λ³€μˆ˜λ₯Ό 직접 μ°Έμ‘°ν•œλ‹€.

 

 

<값에 μ˜ν•œ 전달>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace Hello
{
    class MainApp
    {
        public static void Swap(int a, int b)
        {
            int temp = b;
            b = a;
            a = temp;
            WriteLine($"Swapλ©”μ†Œλ“œ λ‚΄μ—μ„œ aλŠ” {a}, bλŠ” {b}");
        }
        static void Main(string[] args)
        {
            int x = 7;
            int y = 5;
            WriteLine($"Mainλ©”μ†Œλ“œ λ‚΄μ—μ„œ xλŠ” {x}, yλŠ” {y}");
            Swap(x,y);
            WriteLine($"Swapλ©”μ†Œλ“œ 호좜 λ’€, Mainλ©”μ†Œλ“œ λ‚΄μ—μ„œ xλŠ” {x}, yλŠ” {y}");

        }
    }
}

<좜λ ₯ κ²°κ³Ό>

Mainλ©”μ†Œλ“œ λ‚΄μ—μ„œ xλŠ” 7, yλŠ” 5
Swapλ©”μ†Œλ“œ λ‚΄μ—μ„œ aλŠ” 5, bλŠ” 7
Swapλ©”μ†Œλ“œ 호좜 λ’€, Mainλ©”μ†Œλ“œ λ‚΄μ—μ„œ xλŠ” 7, yλŠ” 5

μœ„ Swap ν•¨μˆ˜μ—μ„œ λ§€κ°œλ³€μˆ˜ aλŠ” xκ°€ κ°€μ§„ 것과 λ˜‘κ°™μ€ 데이터λ₯Ό κ°–κ³  μžˆμ§€λ§Œ,

a와 xλŠ” μ™„μ „νžˆ λ³„κ°œμ˜ λ©”λͺ¨λ¦¬ 곡간을 μ‚¬μš©ν•œλ‹€. b와 y의 관계도 λ§ˆμ°¬κ°€μ§€μ΄λ‹€. (마치 μ•„λž˜ ν‘œμ™€ 같이)

b=5
a=7
y=5
x=7

 

좜λ ₯ κ²°κ³Όλ₯Ό 보면

호좜이 λλ‚œ ν›„ x와 y의 값은 μ—¬μ „ν•˜λ‹€.

λ©”μ†Œλ“œκ°€ 맀개 λ³€μˆ˜λ₯Ό 받아듀일 λ•ŒλŠ” λ°μ΄ν„°μ˜ '볡사'κ°€ 이루어진닀.

Swap() λ©”μ†Œλ“œμ˜ 첫 번째 맀개 λ³€μˆ˜ aλŠ” xκ°€ λ‹΄κ³  μžˆλŠ” 7을 λ³΅μ‚¬ν•΄μ„œ λ°›κ³ ,

λ‘λ²ˆμ§Έ λ§€κ°œλ³€μˆ˜ bλŠ” yκ°€ λ‹΄κ³  μžˆλŠ” 5λ₯Ό λ³΅μ‚¬ν•΄μ„œ λ°›λŠ”λ‹€.

Swap() λ©”μ†Œλ“œ μ•ˆμ—μ„œ 아무리 두 맀개 λ³€μˆ˜λ₯Ό μš”λ¦¬μ‘°λ¦¬ μš”λ¦¬ν•΄λ„

Main() λ©”μ†Œλ“œ μ•ˆμ—μ„œ μ„ μ–Έλœ x와 yμ—λŠ” 영ν–₯이 μ—†λŠ” 것이닀.

이처럼 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œ 데이터λ₯Ό λ³΅μ‚¬ν•΄μ„œ λ§€κ°œλ³€μˆ˜μ— λ„˜κΈ°λŠ” 것을 "값에 μ˜ν•œ 전달"이라고 λΆ€λ₯Έλ‹€. 

 

<참쑰에 μ˜ν•œ λ§€κ°œλ³€μˆ˜ 전달>

Swap() λ©”μ†Œλ“œκ°€ 이름값을 ν•˜λ„λ‘,

즉 두 λ§€κ°œλ³€μˆ˜μ˜ 값을 μ œλŒ€λ‘œ κ΅ν™˜ν•  수 μžˆλ„λ‘ ν•˜κΈ° μœ„ν•΄μ„œλŠ”

맀개 λ³€μˆ˜λ₯Ό "참쑰에 μ˜ν•œ 전달"둜 λ„˜κΈ°λ©΄ λœλ‹€.

 

값에 μ˜ν•œ 전달이 λ§€κ°œλ³€μˆ˜κ°€ λ³€μˆ˜λ‚˜ μƒμˆ˜λ‘œλΆ€ν„° 값을 λ³΅μ‚¬ν•˜λŠ” 것과 달리,

참쑰에 μ˜ν•œ 전달은 λ§€κ°œλ³€μˆ˜κ°€ λ©”μ†Œλ“œμ— λ„˜κ²¨μ§„ 원본 λ³€μˆ˜λ₯Ό 직접 μ°Έμ‘°ν•œλ‹€.

 

λ”°λΌμ„œ λ©”μ†Œλ“œ μ•ˆμ—μ„œ 맀개 λ³€μˆ˜λ₯Ό μˆ˜μ •ν•˜λ©΄

이 λ§€κ°œλ³€μˆ˜κ°€ μ°Έμ‘°ν•˜κ³  μžˆλŠ” 원본 λ³€μˆ˜μ— μˆ˜μ •μ΄ 이루어진닀.

 

C#μ—μ„œ 참쑰에 μ˜ν•œ 맀개 λ³€μˆ˜ 전달은 μ•„μ£Ό 쉽닀.

ref ν‚€μ›Œλ“œλ₯Ό λ§€κ°œλ³€μˆ˜ μ•žμ— 뢙이면 λœλ‹€.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace Hello
{
    class MainApp
    {
        public static void Swap(ref int a, ref int b)
        {
            int temp = b;
            b = a;
            a = temp;
            WriteLine($"Swapλ©”μ†Œλ“œ λ‚΄μ—μ„œ aλŠ” {a}, bλŠ” {b}");
        }
        static void Main(string[] args)
        {
            int x = 7;
            int y = 5;
            WriteLine($"Mainλ©”μ†Œλ“œ λ‚΄μ—μ„œ xλŠ” {x}, yλŠ” {y}");
            Swap(ref x,ref y);
            WriteLine($"Swapλ©”μ†Œλ“œ 호좜 λ’€, Mainλ©”μ†Œλ“œ λ‚΄μ—μ„œ xλŠ” {x}, yλŠ” {y}");

        }
    }
}

이제 aλŠ” xλ₯Ό μ°Έμ‘°ν•˜κ³ ,

bλŠ” yλ₯Ό μ°Έμ‘°ν•˜κ²Œ λ˜μ—ˆλ‹€.

 

 

<좜λ ₯ κ²°κ³Ό>

Mainλ©”μ†Œλ“œ λ‚΄μ—μ„œ xλŠ” 7, yλŠ” 5
Swapλ©”μ†Œλ“œ λ‚΄μ—μ„œ aλŠ” 5, bλŠ” 7
Swapλ©”μ†Œλ“œ 호좜 λ’€, Mainλ©”μ†Œλ“œ λ‚΄μ—μ„œ xλŠ” 5, yλŠ” 7

 

<λ©”μ†Œλ“œμ˜ κ²°κ³Όλ₯Ό 참쑰둜 λ°˜ν™˜ν•˜κΈ° ref> : λ©”μ†Œλ“œμ˜ κ²°κ³Όλ₯Ό 참쑰둜 λ°˜ν™˜ν•˜λŠ” μ°Έμ‘° λ°˜ν™˜κ°’

μ°Έμ‘° λ°˜ν™˜κ°’μ„ μ΄μš©ν•˜λ©΄ λ©”μ†Œλ“œμ˜ 호좜자둜 ν•˜μ—¬κΈˆ λ°˜ν™˜λ°›μ€ κ²°κ³Όλ₯Ό 참쑰둜 λ‹€λ£° 수 μžˆλ„λ‘ ν•œλ‹€.

<예제 1>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace Hello
{
    class Product
    {
        private int price = 100;
        public ref int GetPrice()
        {
            return ref price;
        }
        public void PrintPrice()
        {
            WriteLine($"Price:{price}");
        }
    }
    class MainApp
    {
        static void Main(string[] args)
        {
            Product carrot = new Product();
            ref int ref_local_price = ref carrot.GetPrice(); //ref_local_priceλ₯Ό μˆ˜μ •ν•˜λ©΄ carrot.price의 λ‚΄μš©λ„ 바뀐닀.
            int normal_local_price = carrot.GetPrice();

            carrot.PrintPrice();
            WriteLine($"Ref Local Price : {ref_local_price}");
            WriteLine($"Normal Local Price : {normal_local_price}");

            ref_local_price = 200;

            carrot.PrintPrice();
            WriteLine($"Ref Local Price : {ref_local_price}");
            WriteLine($"Normal Local Price :{normal_local_price}");
        }
    }
}

 <좜λ ₯>

Price:100
Ref Local Price : 100
Normal Local Price : 100
Price:200
Ref Local Price : 200
Normal Local Price :100

<μ—°μŠ΅1>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace Hello
{
    class Product
    {
        private int price = 100;
        public int Price
        {
            get { return price; }
            set { price = value; }
        }
        public int Sale()
        {
            price = (price / 2); 
            return price;
        }
        public void PrintPrice()
        {
            WriteLine(price);
        }
    }
    class MainApp
    {
        static void Main(string[] args)
        {
            Product carrot = new Product();
            int carrot_price = carrot.Price;
            WriteLine(carrot_price);//100
            carrot.Sale();
            carrot.PrintPrice(); //50
            carrot_price = carrot.Price;
            WriteLine(carrot_price);//50
        }
    }
}

<μ—°μŠ΅2>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace Hello
{
    class Product
    {
        private int price = 100;
        public ref int getPrice()
        {
            return ref price;
        }
        public int Sale()
        {
            price = (price / 2); 
            return price;
        }
        public void PrintPrice()
        {
            WriteLine(price);
        }
    }
    class MainApp
    {
        static void Main(string[] args)
        {
            Product carrot = new Product();
            int carrot_price1 = carrot.getPrice();
            WriteLine(carrot_price1);//100

            carrot.Sale();
            carrot.PrintPrice(); //50

            carrot_price1 = carrot.getPrice();
            WriteLine(carrot_price1);//50
            ref int carrot_price2 = ref carrot.getPrice();
            WriteLine(carrot_price2);//50

            carrot_price1 *= 2;
            carrot.PrintPrice(); //50
            carrot_price2 *= 2;
            carrot.PrintPrice(); //100
        }
    }
}

<좜λ ₯ μ „μš© λ§€κ°œλ³€μˆ˜ out >

λŒ€κ°œμ˜ 경우 λ©”μ†Œλ“œμ˜ κ²°κ³ΌλŠ” ν•˜λ‚˜λ§Œ μΆ©λΆ„ν•˜λ‹€. λ‹€λ§Œ, λ‘κ°œ μ΄μƒμ˜ κ²°κ³Όλ₯Ό μš”κ΅¬ν•˜λŠ” νŠΉλ³„ν•œ λ©”μ†Œλ“œλ“€μ΄ μžˆλ‹€.

λ‚˜λˆ—μ…ˆμ„ κ΅¬ν˜„ν•  λ•ŒλŠ” μ œμˆ˜μ™€ ν”Όμ œμˆ˜λ₯Ό λ§€κ°œλ³€μˆ˜λ‘œ λ„˜κ²¨λ°›κ³  

κ²°κ³ΌλŠ” λͺ«κ³Ό λ‚˜λ¨Έμ§€ 두 개둜 λ°˜ν™˜ν•  ν•„μš”κ°€ μžˆλ‹€.

 

λ‹€μŒκ³Ό 같이 ref ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•΄μ„œ λ©”μ†Œλ“œλ₯Ό κ΅¬ν˜„ν•˜λ©΄ λͺ«κ³Ό λ‚˜λ¨Έμ§€λ₯Ό ν•œλ²ˆμ— λ°˜ν™˜ν•  수 μžˆλ‹€.

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static void Divide(int a, int b, ref int quotient, ref int remainder)
        {
            quotient = 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);
            WriteLine($"a:{a},b:{b},a/b:{c},a%b:{d}");
        }
    }
}

좜λ ₯ κ²°κ³Ό

a:20,b:3,a/b:6,a%b:2

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        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);
            WriteLine($"a:{a},b:{b},a/b:{c},a%b:{d}");
        }
    }
}

 

좜λ ₯κ²°κ³Ό

a:20,b:3,a/b:6,a%b:2

----

μ—°μŠ΅

----

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static void Divide(int ori, int div, ref int mox, ref int namuge)
        {
            mox = ori / div;
            namuge = ori % div;
        }
        static void Main(string[] args)
        {
            int a = 20;
            int b = 6;
            int c = 0;
            int d = 0;
            Divide(a,b,ref c,ref d);
            WriteLine($"aλŠ” {a}, bλŠ” {b}, cλŠ” {c}, dλŠ” {d}");
        }
    }
}

좜λ ₯ κ²°κ³Ό

aλŠ” 20, bλŠ” 6, cλŠ” 3, dλŠ” 2

μœ„μ™€ 같이 refλ§ŒμœΌλ‘œλ„ μ—¬λŸ¬ 개의 κ²°κ³Όλ₯Ό λ©”μ†Œλ“œμ—μ„œ μ–»μ–΄μ˜¬ 수 μžˆμ§€λ§Œ,

C#은 더 μ•ˆμ „ν•œ λ°©λ²•μœΌλ‘œ λ˜‘κ°™μ€ 일을 κ°€λŠ₯μΌ€ ν•΄μ€€λ‹€.

λ°”λ‘œ out ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•œ "좜λ ₯ μ „μš© λ§€κ°œλ³€μˆ˜"κ°€ λ°”λ‘œ 그것이닀.

out ν‚€μ›Œλ“œμ˜ μ‚¬μš©λ²•μ€ κ°„λ‹¨ν•˜λ‹€.

λ©”μ†Œλ“œμ˜ 선언뢀와 ν˜ΈμΆœλΆ€μ— ref ν‚€μ›Œλ“œ λŒ€μ‹ μ— out ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 전뢀이닀.

 

λ°”λ‘œ μ•„λž˜μ™€ 같이 μž‘μ„±ν•˜λ©΄ λœλ‹€!

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static void Divide(int ori, int div, out int mox, out int namuge)
        {
            mox = ori / div;
            namuge = ori % div;
        }
        static void Main(string[] args)
        {
            int a = 20;
            int b = 6;
            int c = 0;
            int d = 0;
            Divide(a,b, out c, out d);
            WriteLine($"aλŠ” {a}, bλŠ” {b}, cλŠ” {c}, dλŠ” {d}");//aλŠ” 20, bλŠ” 6, cλŠ” 3, dλŠ” 2
        }
    }
}

μ–Έλœ» 보면 ν‚€μ›Œλ“œλ§Œ λ‹¬λΌμ‘Œλ‹€ 뿐이지 달라진 것이 없어보인닀.

ν•˜μ§€λ§Œ outμ—λŠ” refμ—λŠ” μ—†λŠ” μ•ˆμ „μž₯μΉ˜κ°€ μžˆλ‹€.

예λ₯Όλ“€μ–΄ ref ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•΄μ„œ λ§€κ°œλ³€μˆ˜λ₯Ό λ„˜κΈ°λŠ” 경우

λ©”μ†Œλ“œκ°€ ν•΄λ‹Ή λ§€κ°œλ³€μˆ˜μ— κ²°κ³Όλ₯Ό μ €μž₯ν•˜μ§€ μ•Šμ•„λ„ μ»΄νŒŒμΌλŸ¬λŠ” μ•„λ¬΄λŸ° κ²½κ³ λ₯Ό ν•˜μ§€ μ•ŠλŠ”λ‹€.

μ΄μ™€λŠ” 달리, out ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•΄μ„œ λ§€κ°œλ³€μˆ˜λ₯Ό λ„˜κΈΈ λ•ŒλŠ”

λ§€μ†Œλ“œκ°€ ν•΄λ‹Ή λ§€κ°œλ³€μˆ˜μ— κ²°κ³Όλ₯Ό μ €μž₯ν•˜μ§€ μ•ŠμœΌλ©΄ μ»΄νŒŒμΌλŸ¬κ°€ μ—λŸ¬ λ©”μ‹œμ§€λ₯Ό 좜λ ₯ν•œλ‹€.

<λΉŒλ“œ 였λ₯˜: μ•„λž˜μ˜ μ½”λ“œλŠ” 싀행이 μ•ˆλ¨!>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static void Divide(int ori, int div, out int mox, out int namuge)
        {
            mox = ori / div;
            //namuge = ori % div;
        }
        static void Main(string[] args)
        {
            int a = 20;
            int b = 6;
            Divide(a,b, out int c, out int d);
            WriteLine($"aλŠ” {a}, bλŠ” {b}, cλŠ” {c}, dλŠ” {d}");//aλŠ” 20, bλŠ” 6, cλŠ” 3, dλŠ” 2
        }
    }
}

<λΉŒλ“œ 였λ₯˜κ°€ μ•ˆ λ‚  λΏλ”λŸ¬ d값이 0으둜 λ‚˜μ˜¨λ‹€>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static void Divide(int ori, int div, out int mox, ref int namuge)
        {
            mox = ori / div;
            //namuge = ori % div;
        }
        static void Main(string[] args)
        {
            int a = 20;
            int b = 6;
            int d=0;
            Divide(a,b, out int c, ref d);
            WriteLine($"aλŠ” {a}, bλŠ” {b}, cλŠ” {c}, dλŠ” {d}");//aλŠ” 20, bλŠ” 6, cλŠ” 3, dλŠ” 0
        }
    }
}

κ·ΈλŸ¬λ―€λ‘œ μ•„λž˜μ™€ 같이 μž‘μ„±ν•˜λŠ” 것이 λ¬΄λ‚œν•˜λ‹€.

좜λ ₯ μ „μš© λ§€κ°œλ³€μˆ˜λŠ” 사싀 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜κΈ° 전에 미리 μ„ μ–Έν•  ν•„μš”κ°€ μ—†λ‹€.

ν˜ΈμΆœν•  λ•Œ λ§€κ°œλ³€μˆ˜ λͺ©λ‘ μ•ˆμ—μ„œ μ¦‰μ„μœΌλ‘œ μ„ μ–Έν•˜λ©΄ 되기 λ•Œλ¬Έμ΄λ‹€.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static void Divide(int ori, int div, out int mox, out int namuge)
        {
            mox = ori / div;
            namuge = ori % div;
        }
        static void Main(string[] args)
        {
            int a = 20;
            int b = 6;
            Divide(a,b, out int c, out int d);
            WriteLine($"aλŠ” {a}, bλŠ” {b}, cλŠ” {c}, dλŠ” {d}");//aλŠ” 20, bλŠ” 6, cλŠ” 3, dλŠ” 2
        }
    }
}

ν˜Ήμ€ μ΄λ ‡κ²Œ

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static void Divide(int ori, int div, out int mox, out int namuge) 
        {
            mox = ori / div;
            namuge = ori % div;
        }
        static void Main(string[] args)
        {
            int a = 20;
            int b = 6;
            int c = 0;
            int d = 0;
            Divide(a, b, out c, out d);
            WriteLine($"aλŠ” {a}, bλŠ” {b}, cλŠ” {c}, dλŠ” {d}");//aλŠ” 20, bλŠ” 6, cλŠ” 3, dλŠ” 2
        }
    }
}

ν•œνŽΈ...

λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜λŠ” μͺ½μ—μ„œλŠ” μ΄ˆκΈ°ν™”ν•˜μ§€ μ•Šμ€ μ§€μ—­ λ³€μˆ˜λ₯Ό λ©”μ†Œλ“œμ˜ out 맀개 λ³€μˆ˜λ‘œ λ„˜κΈΈ 수 μžˆλ‹€. 

μ»΄νŒŒμΌλŸ¬κ°€ ν˜ΈμΆœλ‹Ήν•˜λŠ” λ©”μ†Œλ“œμ—μ„œ κ·Έ μ§€μ—­ λ³€μˆ˜λ₯Ό ν• λ‹Ήν•  것을 보μž₯ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€. 

이처럼 컴파일러λ₯Ό 톡해 κ²°κ³Όλ₯Ό ν• λ‹Ήν•˜μ§€ μ•ŠλŠ” 버그가 λ§Œλ“€μ–΄μ§ˆ κ°€λŠ₯성을 μ œκ±°ν•  수 μžˆλ‹€λ©΄

κ·Έ 방법을 μ‚¬μš©ν•˜λŠ” 것이 μ’‹λ‹€.

λŸ°νƒ€μž„μ— λ°œμƒν•˜λŠ” λ²„κ·ΈλŠ” 컴파일 νƒ€μž„μ— λ°œμƒν•˜λŠ” 버그보닀 훨씬 μž‘κΈ°κ°€ μ–΄λ ΅κΈ° λ•Œλ¬Έμ΄λ‹€.

(컴파일 μ—λŸ¬λŠ” μ–΄λŠ 곳에 λ¬Έμ œκ°€ μžˆλŠ”μ§€ μ»΄νŒŒμΌλŸ¬κ°€ μ •ν™•ν•˜κ²Œ μ•Œλ €μ£Όμ§€λ§Œ,

λŸ°νƒ€μž„ λ²„κ·ΈλŠ” ν”„λ‘œκ·Έλž˜λ¨Έμ˜ 논리λ ₯으둜 좔적해야 ν•œλ‹€.)

 

<λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”©>

μ˜€λ²„λ‘œλ”© Overloading μ΄λž€ "κ³Όμ ν•˜λ‹€"λΌλŠ” λœ»μ„ κ°€μ§€κ³  μžˆλ‹€.

κ³Όμ μ΄λž€ 트럭 λ”°μœ„μ— μ›λž˜μ˜ νƒ‘μž¬λŸ‰μ„ λ„˜κ²¨ μ‹£λŠ” 것을 λ§ν•œλ‹€.

λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”©μ€ ν•˜λ‚˜μ˜ λ©”μ†Œλ“œ 이름에 μ—¬λŸ¬ 개의 κ΅¬ν˜„μ„ μ˜¬λ¦¬λŠ” 것을 λœ»ν•œλ‹€.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static int Plus(int a, int b) 
        {
            return a + b;
        }
        static int Plus(int a, int b, int c)
        {
            return a + b + c;
        }
        static double Plus(double a, double b)
        {
            return a + b;
        }
        static double Plus(double a, double b, double c)
        {
            return a + b + c;
        }
        static void Main(string[] args)
        {
            int a = Plus(1, 2);
            WriteLine(a);
            WriteLine(Plus(1, 2, 3));
            WriteLine(Plus(1.1, 2.1));
            WriteLine(Plus(1.1, 2.1,3.1));
        }
    }
}

μœ„ μ½”λ“œμ™€ 같이 μ˜€λ²„λ‘œλ”©μ„ ν•΄λ†“μœΌλ©΄ μ»΄νŒŒμΌλŸ¬κ°€ λ©”μ†Œλ“œ 호좜 μ½”λ“œμ— μ‚¬μš©λ˜λŠ”

λ§€κ°œλ³€μˆ˜μ˜ μˆ˜μ™€ ν˜•μ‹μ„ λΆ„μ„ν•΄μ„œ(μ˜€λ‘œμ§€ λ§€κ°œλ³€μˆ˜λ§Œ λΆ„μ„ν•œλ‹€. λ°˜ν™˜ ν˜•μ‹μ€ 묻지도 따지지도 μ•ŠλŠ”λ‹€.)

μ–΄λ–€ 버전이 ν˜ΈμΆœλ μ§€λ₯Ό μ°Ύμ•„μ€€λ‹€.

μ‹€ν–‰ν•  λ©”μ†Œλ“œμ˜ 버전을 μ°ΎλŠ” μž‘μ—…μ΄ 컴파일 νƒ€μž„μ— μ΄λ£¨μ–΄μ§€λ―€λ‘œ μ„±λŠ₯ μ €ν•˜λŠ” κ±±μ •ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€.

λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”©μ€ 이름에 λŒ€ν•œ 고민을 μ€„μ—¬μ£ΌλŠ” λ™μ‹œμ—

μ½”λ“œλ₯Ό μΌκ΄€μ„±μžˆκ²Œ μœ μ§€ν•΄μ€€λ‹€.

(μΌκ΄€μ„±μžˆλŠ” μ½”λ“œλŠ” λ©”μ†Œλ“œ μž‘μ„±μžμ—κ²Œλ„ 도움을 μ£Όμ§€λ§Œ, λ©”μ†Œλ“œ μ‚¬μš©μžμ—κ²Œλ„ 높은 생산성을 μ œκ³΅ν•œλ‹€.)

예λ₯Ό λ“€μ–΄ System.Console 클래슀의 WriteLine() λ©”μ†Œλ“œλŠ” λͺ¨λ‘ 18개의 버전을 μ˜€λ²„λ‘œλ”©ν•˜κ³  μžˆμ§€λ§Œ

ν”„λ‘œκ·Έλž˜λ¨ΈλŠ” 버전을 μ˜μ‹ν•˜μ§€ μ•Šκ³  자유둭게 이λ₯Ό μ‚¬μš©ν•΄μ™”λ‹€.

λ§Œμ•½ μ˜€λ²„λ‘œλ”©μ΄ μ—†μ—ˆλ‹€λ©΄ WriteLine() λ©”μ†Œλ“œλŠ” κ°€ κ΅¬ν˜„μ— 따라 18개의 μ΄λ¦„μœΌλ‘œ λ‚˜λ‰˜μ—ˆμ„ 것이닀.

 

<κ°€λ³€ 개수의 인수 Params ν‚€μ›Œλ“œμ™€ λ°°μ—΄>

ν”„λ‘œκ·Έλž˜λ°μ„ ν•˜λ‹€λ³΄λ©΄ κ·Έμ € 인수의 κ°œμˆ˜κ°€ λ‹€λ₯΄λ‹€λŠ” 이유만으둜 λ˜‘κ°™μ€ λ©”μ†Œλ“œλ₯Ό μ—¬λŸ¬κ°€μ§€ λ²„μ „μœΌλ‘œ μ˜€λ²„λ‘œλ”©ν•˜κ³  싢을 λ•Œκ°€ μžˆλ‹€. 이런 경우λ₯Ό μœ„ν•΄ C#은 κ°€λ³€ 개수의 μΈμˆ˜λΌλŠ” κΈ°λŠ₯을 μ œκ³΅ν•œλ‹€.

κ°€λ³€ 개수의 μΈμˆ˜λž€, κ·Έ κ°œμˆ˜κ°€ μœ μ—°ν•˜κ²Œ λ³€ν•  수 μžˆλŠ” 인수λ₯Ό λ§ν•œλ‹€.

이것을 μ΄μš©ν•˜λ©΄ λ‹€μŒκ³Ό 같이 μž…λ ₯λ˜λŠ” λͺ¨λ“  인수의 합을 κ΅¬ν•˜λŠ” Sum() λ©”μ†Œλ“œλ₯Ό μ˜€λ²„λ‘œλ”©ν•˜μ§€ μ•Šκ³ λ„ κ΅¬ν˜„ν•  수 μžˆλ‹€.

 

int total = 0;

total = Sum(1,2);
total = Sum(1,2,3);
total = Sum(1,2,3,4,5,6,7,8,9,10);
//...

κ°€λ³€ 개수의 μΈμˆ˜λŠ” params ν‚€μ›Œλ“œμ™€ 배열을 μ΄μš©ν•΄μ„œ μ„ μ–Έν•œλ‹€.

λ‹€μŒμ€ κ°€λ³€ 개수의 인수λ₯Ό μ΄μš©ν•΄μ„œ λͺ¨λ“  인수의 합을 ꡬ해 λ°˜ν™˜ν•˜λŠ” Sum() λ©”μ†Œλ“œμ˜ κ΅¬ν˜„μ΄λ‹€.

μ΄λ ‡κ²Œ κ΅¬ν˜„ν•œ λ©”μ†Œλ“œλŠ” μ•žμ—μ„œ λ³Έ μ½”λ“œμ—μ„œμ²˜λŸΌ 인수의 개수λ₯Ό λ‹¬λ¦¬ν•΄μ„œ ν˜ΈμΆœν•  수 μžˆλ‹€.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static int Sum(params int[] args)
        {
            int sum = 0;
            for(int i=0; i<args.Length; i++)
            {
                if (i > 0) Write(", ");
                Write($"{args[i]}");
                sum += args[i];
            }
            WriteLine();
            return sum;
        }
        static void Main(string[] args)
        {
            int sum = Sum(3, 4, 5, 6, 7, 8, 9, 10);
            WriteLine($"Sum : {sum}");
        }
    }
}

좜λ ₯κ°’

3, 4, 5, 6, 7, 8, 9, 10
Sum : 52

 

λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”©κ³Ό κ°€λ³€ 개수의 μΈμˆ˜λŠ” ν™œμš©μ„±μ—μ„œ 차이가 μžˆλ‹€.

λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”©μ€ λ§€κ°œλ³€μˆ˜μ˜ 개수 뿐 μ•„λ‹ˆλΌ ν˜•μ‹μ΄ λ‹€λ₯Έ κ²½μš°μ— μ‚¬μš©ν•  수 μžˆλ‹€.

λ˜ν•œ λ§€κ°œλ³€μˆ˜μ˜ κ°œμˆ˜κ°€ μœ ν•œν•˜κ²Œ μ •ν•΄μ Έ μžˆλ‹€λ©΄ κ°€λ³€ 개수의 μΈμˆ˜λ³΄λ‹€λŠ”

λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”©μ„ μ‚¬μš©ν•˜λŠ” 것이 μ μ ˆν•˜λ‹€.

κ°€λ³€ 개수의 μΈμˆ˜λŠ” ν˜•μ‹μ€ κ°™μœΌλ‚˜ 인수의 개수만 μœ μ—°ν•˜κ²Œ λ‹¬λΌμ§ˆ 수 μž‡λŠ” κ²½μš°μ— μ ν•©ν•˜λ‹€.

 

맀개 λ³€μˆ˜μ˜ κ°œμˆ˜κ°€ μœ μ—°ν•˜κ²Œ λ³€ν•  λ•Œ -> κ°€λ³€ 개수의 인수

맀개 λ³€μˆ˜μ˜ κ°œμˆ˜κ°€ μœ ν•œν•˜κ³ , ν˜•μ‹μ„ 달리 ν•΄μ•Όν•  겨우 -> λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”© 

 

<λͺ…λͺ…λœ 인수> : 가독성을 μ˜¬λ¦¬λŠ” κΈ°λ²•μœΌλ‘œ,

말 κ·ΈλŒ€λ‘œ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œ 인수의 이름에 κ·Όκ±°ν•΄μ„œ 데이터λ₯Ό ν• λ‹Ήν•  수 μžˆλŠ” κΈ°λŠ₯이닀.

λͺ…λͺ…λœ 인수λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄ λ©”μ†Œλ“œ 선언에 μ†λŒˆ 일은 μ „ν˜€ μ—†λ‹€.

λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œλ§Œ 인수의 이름 뒀에 콜둠(:)을 뢙인 λ’€ κ·Έ 뒀에 ν• λ‹Ήν•  데이터λ₯Ό λ„£μ–΄μ£Όλ©΄ λœλ‹€.

λͺ…λͺ…λœ μΈμˆ˜λŠ” 더 λ§Žμ€ 타이핑을 ν•΄μ•Όν•˜λ―€λ‘œ 생산성 λ©΄μ—μ„œλŠ” λ–¨μ–΄μ§€λ‚˜,

일단 λͺ…λͺ…λœ 인수λ₯Ό μ΄μš©ν•΄μ„œ μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ©΄ 가독성이 μ˜¬λΌκ°„λ‹€.(μ„ νƒμ˜ 문제)

κ·ΈλŸ¬λ―€λ‘œ, μΈμˆ˜κ°€ λ„ˆλ¬΄ λ§Žμ•„ μ–΄λŠ λ§€κ°œλ³€μˆ˜μ— μ–΄λŠ 인수λ₯Ό ν• λ‹Ήν•˜κ³  μžˆλŠ”μ§€ 뢄간이 μ–΄λ €μš΄ κ²½μš°μ—λŠ” λͺ…λͺ…λœ μΈμˆ˜κ°€ 도움이 될 것이닀.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static void PrintProfile(string name, string phone)
        {
            WriteLine($"Name: {name}, Phone: {phone}");
        }
        static void Main(string[] args)
        {
            PrintProfile(name:"λ°•μ°¬ν˜Έ",phone:"010-123-1234");
            PrintProfile(phone:"010-9876-5432", name: "박세리");
            PrintProfile("κΉ€μ—°μ•„", "010-987-654");
            PrintProfile("μž₯λ―Έλž€", phone:"010-234-567");
        }
    }
}

 

<둜컬 ν•¨μˆ˜>

둜컬 ν•¨μˆ˜λŠ” λ©”μ†Œλ“œ μ•ˆμ—μ„œ μ„ μ–Έλ˜κ³ , μ„ μ–Έλœ λ©”μ†Œλ“œ μ•ˆμ—μ„œλ§Œ μ‚¬μš©λ˜λŠ” νŠΉλ³„ν•œ ν•¨μˆ˜μ΄λ‹€.

클래슀의 멀버가 μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— λ©”μ†Œλ“œκ°€ μ•„λ‹ˆλΌ ν•¨μˆ˜λΌκ³  λΆ€λ₯Έλ‹€.

μ„ μ–Έ 방법은 λ©”μ†Œλ“œμ™€ λ‹€λ₯΄μ§€ μ•Šμ§€λ§Œ

둜컬 ν•¨μˆ˜λŠ” μžμ‹ μ΄ μ‘΄μž¬ν•˜λŠ” 지역에 μ„ μ–Έλ˜μ–΄ μžˆλŠ” λ³€μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.

둜컬 ν•¨μˆ˜λŠ” λ©”μ†Œλ“œ λ°–μ—μ„œλŠ” λ‹€μ‹œ μ“Έ 일 μ—†λŠ” 반볡적인 μž‘μ—…μ„ ν•˜λ‚˜μ˜ 이름 μ•„λž˜ λ¬Άμ–΄λ†“λŠ” 데 μ œκ²©μ΄λ‹€.

λžŒλ‹€μ‹κ³Ό λ”λΆˆμ–΄ ν”„λ‘œκ·Έλž˜λ¨Έμ—κ²Œ μ½”λ“œλ₯Ό 간좔릴 수 μžˆλŠ” 또 ν•˜λ‚˜μ˜ μ˜΅μ…˜μΈ 것이닀.

class SoneClass
{
	public void SomeMethod() //λ©”μ†Œλ“œ μ„ μ–Έ
    {
    	int count = 0;
        SomeLocalFunction(1,2); //둜컬 ν•¨μˆ˜ 호좜
        SomeLocalFunction(3,4);
        
        void SomeLocalFunction(int a, int b) //둜컬 ν•¨μˆ˜ μ„ μ–Έ
        {
        	//Do Some Work
        	Write($"count : {++count}"); 
            //둜컬 ν•¨μˆ˜λŠ” μžμ‹ μ΄ μ†ν•œ λ©”μ†Œλ“œμ˜ μ§€μ—­ λ³€μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.
		}
    }
}

 

<예제 ν”„λ‘œκ·Έλž¨>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static string ToLowerString(string input)
        {
            var arr = input.ToCharArray();//https://www.delftstack.com/ko/howto/csharp/csharp-convert-string-to-char/
            //var은 char[]둜 λ³€ν™˜λœλ‹€
            //string str = "String";
            //char[] charArray = str.ToCharArray();
            for (int i=0; i<arr.Length; i++)
            {
                arr[i] = ToLowerChar(i);
            }
            char ToLowerChar(int i)
            {
                if (arr[i] < 65 || arr[i] > 90)//A~Z의 μ•„μŠ€ν‚€κ°’ : 65~99
                    return arr[i];
                else //a~z의 μ•„μŠ€ν‚€κ°’ : 97~122
                    return (char)(arr[i] + 32);
            }
            return new string(arr);
        }
        static void Main(string[] args)
        {
            WriteLine(ToLowerString("Hello!"));
            WriteLine(ToLowerString("Good Morning!"));
            WriteLine(ToLowerString("This is C#!"));
            string str = "String";
            char[] charArray = str.ToCharArray();
            WriteLine(charArray);
            WriteLine(charArray[0]);
        }
    }
}

<좜λ ₯κ²°κ³Ό>

hello!
good morning!
this is c#!
String
S
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        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) //A~Z의 μ•„μŠ€ν‚€κ°’ : 65~90, a~z의 μ•„μŠ€ν‚€κ°’ : 97~122
                    return arr[i]; //즉 λŒ€λ¬Έμžκ°€ μ•„λ‹ˆλΌλ©΄ κ·ΈλŒ€λ‘œ 
                else //λŒ€λ¬ΈμžλΌλ©΄
                    return (char)(arr[i] + 32); //32λ₯Ό λ”ν•΄μ„œ λŒ€λ¬Έμžλ‘œ λ°”κΎΈμ–΄μ€€λ‹€.
            }
            return new string(arr);
        }
        static void Main(string[] args)
        {
            WriteLine(ToLowerString("Hello!"));
            WriteLine(ToLowerString("Good Morning!"));
            WriteLine(ToLowerString("This is C#."));
        }
    }
}

좜λ ₯κ²°κ³Ό

hello!
good morning!
this is c#.

<이것이 C#이닀 챕터 6 μ—°μŠ΅λ¬Έμ œ 1번>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static double Square(double arg)
        {
            double result = arg * arg;
            return result;
        }
        static void Main(string[] args)
        {
            Write("수λ₯Ό μž…λ ₯ν•˜μ„Έμš” : ");
            string input = ReadLine();
            double arg = Convert.ToDouble(input);
            WriteLine("κ²°κ³Ό : {0}", Square(arg));
        }
    }
}

<이것이 C#이닀 챕터 6 μ—°μŠ΅λ¬Έμ œ 2번>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static void Main(string[] args)
        {
            double mean = 0;
            Mean(1, 2, 3, 4, 5, out mean);
            WriteLine($"평균:{mean}");
        }
        public static void Mean(double a, double b, double c, double d, double e, out double mean)
        {
            mean = (a + b + c + d + e) / 5;
        }
    }
}

<이것이 C#이닀 챕터6 μ—°μŠ΅λ¬Έμ œ 3번>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace UsingOut
{
    class MainApp
    {
        static void Main(string[] args)
        {
            int a = 3;
            int b = 4;
            int resultA = 0;

            Plus(a, b, out resultA);

            WriteLine("{0} + {1}={2}",a,b,resultA);

            double x = 2.4;
            double y = 3.1;
            double resultB = 0;

            Plus(x, y, out resultB); //μ˜€λ²„λ‘œλ“œκ°€ ν•„μš”ν•œ λ©”μ†Œλ“œ
            WriteLine("{0}+{1}={2}",x,y,resultB);
        }

        public static void Plus(int a, int b, out int c)
        {
            c = a + b;
        }
        public static void Plus(double a, double b, out double c)
        {
            c = a + b;
        }
    }
}

 

728x90