250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ์œ„์ž„
  • MySQL
  • ์ปดํฌ์ง€์…˜
  • ใ…ฃใ„ท

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

C#/์ด๊ฒƒ์ด C#์ด๋‹ค

16. ๊ฐ’, ์ฐธ์กฐ์— ์˜ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ „๋‹ฌ, ref์™€ out์˜ ์ฐจ์ด

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

'C# > ์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

18. ๊ฐ์ฒด์˜ ์‚ถ๊ณผ ์ฃฝ์Œ(์ƒ์„ฑ์ž,์ข…๋ฃŒ์ž) + ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ  (0) 2022.02.10
17. C#์˜ ํด๋ž˜์Šค  (0) 2022.02.10
15. ๋ฉ”์†Œ๋“œ๋กœ ์ฝ”๋“œ ๊ฐ„์ถ”๋ฆฌ๊ธฐ + ์žฌ๊ท€ํ˜ธ์ถœ ํ•จ์ˆ˜์งœ๊ธฐ(ํ”ผ๋ณด๋‚˜์น˜ ์˜ˆ์ œ)  (0) 2022.02.09
14. ์ž…์ถœ๋ ฅ, ๋ถ„๊ธฐ๋ฌธ  (0) 2022.02.08
13. null ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž  (0) 2022.02.08
    'C#/์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • 18. ๊ฐ์ฒด์˜ ์‚ถ๊ณผ ์ฃฝ์Œ(์ƒ์„ฑ์ž,์ข…๋ฃŒ์ž) + ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ
    • 17. C#์˜ ํด๋ž˜์Šค
    • 15. ๋ฉ”์†Œ๋“œ๋กœ ์ฝ”๋“œ ๊ฐ„์ถ”๋ฆฌ๊ธฐ + ์žฌ๊ท€ํ˜ธ์ถœ ํ•จ์ˆ˜์งœ๊ธฐ(ํ”ผ๋ณด๋‚˜์น˜ ์˜ˆ์ œ)
    • 14. ์ž…์ถœ๋ ฅ, ๋ถ„๊ธฐ๋ฌธ
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”