16. κ°, μ°Έμ‘°μ μν λ§€κ°λ³μ μ λ¬, 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 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;
}
}
}