๊ฐ์ ์ํ ์ ๋ฌ์ด ๋งค๊ฐ๋ณ์๊ฐ ๋ณ์๋ ์์๋ก๋ถํฐ ๊ฐ์ ๋ณต์ฌํ๋ ๊ฒ๊ณผ ๋ฌ๋ฆฌ,
์ฐธ์กฐ์ ์ํ ์ ๋ฌ์ ๋งค๊ฐ๋ณ์๊ฐ ๋ฉ์๋์ ๋๊ฒจ์ง ์๋ณธ ๋ณ์๋ฅผ ์ง์ ์ฐธ์กฐํ๋ค.
<๊ฐ์ ์ํ ์ ๋ฌ>
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;
}
}
}
'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 |