C# 9.0 - (15) Top-level statements

์ด์ œ C# 9.0์˜ Top-level statements ๋กœ ์ธํ•ด ํ•œ์ค„์งœ๋ฆฌ C# ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

System.Console.WriteLine("Hello C# TLS!");

์˜ค๋Š˜ ์ ๊ทน์ ์œผ๋กœ TLS๋กœ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์งœ๋ดค๋Š”๋ฐ, ๋Š๋‚Œ ๋‚˜์˜์ง€ ์•Š๋„ค์š”. ๊ทธ๋Ÿฐ๋ฐ,
์ €๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์งค ๋•Œ ์ผ๋”ด ์งœ๊ณ  ์ดํ›„์— static ๋ฉ”์†Œ๋“œ๋กœ ๋ฆฌํŒฉํ† ๋ง์„ ํ•˜๋Š”๋ฐ, TLS์ธ ๊ฒฝ์šฐ ์•„์ง ๋ฆฌํŒฉํ† ๋ง(CTRL+.)์ด ๋จน์ง€๋ฅผ ์•Š์Šต๋‹ˆ๋‹ค. (Visual Studio 2019 16.9.0 Preview 1.0 ๊ธฐ์ค€)

Main ๋ฉ”์†Œ๋“œ ์ธ์ž๋Š” ์—ฌ์ „ํžˆ args๋กœ ์ ‘๊ทผ์ด ๋ฉ๋‹ˆ๋‹ค.

Top-level statements๊ฐ€ ๋ญ ๋Œ€๋‹จํ•œ๊ฒŒ ์•„๋‹ˆ๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ์–ธ์–ด๋ฅผ ์ฒ˜์Œ ๋ฐฐ์šฐ๋Š” ์ž…์žฅ์—์„œ ๋ฐฐ์šฐ์ง€๋„ ์•Š์€ using, namespace, class, method๋ฅผ ๋งˆ์ฃผํ•˜๋ฉด์„œ ์–ด๋ ต๋‹ค๊ณ  ๋Š๋‚„ ๊ทธ ์ˆœ๊ฐ„์ด ์–ธ์–ด์˜ ์ฒซ ๋งŒ๋‚จ์€ ์ด์ œ ์•„๋‹ˆ๋ผ๋Š”๊ฒŒ ์ €๋Š” ์ƒ๋‹นํžˆ ๊ณ ๋ฌด์ ์ธ ๋ณ€ํ™”๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค ^^;

์ข‹์•„์š” 1

์ด์ œ REPL ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋งŒ ๋“ค์–ด๊ฐ€๋ฉด ๋”ฑ ์ข‹๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“œ๋„ค์š”. Java์ฒ˜๋Ÿผ์š”. ใ…Žใ…Ž

์ข‹์•„์š” 1

C#์˜ ๋ฐœ์ „ ๋ฐฉํ–ฅ์„ฑ์ด function language์˜ ์žฅ์ ์„ ์ฐจ์šฉํ•˜๊ณ  python๊ณผ ๊ฐ™์ด ๋ฌธ์ œํ•ด๊ฒฐ์—๋งŒ ์ง‘์ค‘ํ•˜๋„๋ก ํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค์ด ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์•„ REPL๋„ ์กฐ๋งŒ๊ฐ„ ํฌํ•จ๋˜์ง€ ์•Š์„๊นŒ ํ•ฉ๋‹ˆ๋‹ค
(๊ธฐ๋Šฅ์ ์œผ๋กœ๋Š” ์‰ฝ๊ฒŒ REPL์„ ๊ตฌํ˜„ํ• ์ˆ˜์žˆ๋„๋ก ์ด๋ฏธ ์ค€๋น„๋œ๊ฒƒ์œผ๋กœ ๋ณด์—ฌ์ง€๋‹ˆ๊นŒ์š”)

REPL์˜ ํŽธ๋ฆฌํ•จ ์ •๋„๊ฐ€ ๋‹ค๋ฅด๊ฒ ์ง€๋งŒ, ์ด๋ฏธ csi.exe๊ฐ€ ์žˆ๊ธด ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜๋Š” ์žˆ์Šต๋‹ˆ๋‹ค.

C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise>csi
Microsoft ยฎ Visual C# Interactive Compiler version 3.8.0-5.20519.18 ()
Copyright ยฉ Microsoft Corporation. All rights reserved.

Type โ€œ#helpโ€ for more information.
> Console.WriteLine(โ€œTESTโ€)
TEST
> int i = 5;
> Console.WriteLine(i)
5

๋‹จ์ง€, ํ•จ์ˆ˜๋ฅผ ํ—ˆ์šฉํ•˜๋Š” ํŒŒ์ด์ฌ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์›Œ๋‚™ ํƒ€์ž… ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฌถ์ธ ๋‹ท๋„ท์ด๋ผ์„œ ๊ฐ„๋‹จํ•œ print ์กฐ์ฐจ๋„ ํƒ€์ดํ•‘์ด ๊ธธ์–ด์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ์•„์ง ๋‚จ์Šต๋‹ˆ๋‹ค. (์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์€ ํŠน๋ณ„ํžˆ interactive ํ™˜๊ฒฝ๋งŒ์„ ์œ„ํ•ด alias ์ฒ˜๋ฆฌ๋ฅผ ํ•ด๋„ ๋˜๊ฒ ์ง€๋งŒโ€ฆ ๊ธ€์Ž„์š”โ€ฆ ^^)

์ข‹์•„์š” 2

MS์—์„œ๋„ REPL ํˆด์„ ์ œ๊ณตํ•œ๊ฒŒ ์žˆ์—ˆ๊ตฐ์š”. ์ €๋Š” ์˜คํ”ˆ ์†Œ์Šค ํ”„๋กœ์ ํŠธ์ด์ง€๋งŒ dotnet-script๊ฐ€ ๊ณต์‹ ํˆด๋กœ ํŽธ์ž…๋œ๋‹ค๋ฉด ์ข‹๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์—ˆ์–ด์š”.

csi.exe๋ฅผ c# REPL ํˆด ์ด๋ผ๊ณ  ํ•˜๋„ค์š”.

@rkttu TLS๊ฐ€ pyhton์˜ ์ง€์œ„, ์˜ˆ๋ฅผ๋“ค์–ด ๊ฐ์ข… ์—ฐ๊ตฌ์†Œ๋˜๋Š” ํ•™๊ต์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ฃผํ”ผํ„ฐ๋…ธํŠธ๋ถ์—์„œ C#์„ ๋™๋“ฑํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ๊ณผ ๊ด€๋ จ์„ฑ ์žˆ์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๊ด€๋ จ ์ฃผ์ œ๋กœ ๋ฐœํ‘œํ•˜์‹œ๋Š” ๊ฒƒ ๊ฐ™์•„ ์‹ ๊ธฐํ•˜๊ณ  ๋ฐ˜๊ฐ€์› ์Šต๋‹ˆ๋‹ค.
TLS์— ๋กœ์ปฌ ๋ฉ”์†Œ๋“œ๋กœ ๋น„์Šทํ•œ ๋Š๋‚Œ์œผ๋กœ ์ฃผํ”ผํ„ฐ๋…ธํŠธ๋ถ์—์„œ ํ™œ์šฉ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์„๊นŒ ์˜ˆ์ƒํ•ด๋ด…๋‹ˆ๋‹ค. ๊ธฐ๋Œ€ ๋ฉ๋‹ˆ๋‹ค ^^

๊ทธ๊ฒƒ์˜ ๊ฐ•๋ ฅํ•œ ์‹œ๊ทธ๋„์€ python๋งŒ๋“  ๋ถ„์ด MS์— ํ•ฉ๋ฅ˜ ํ–ˆ๋‹ค๋Š” ๊ฒƒ

C#์ด ์ ์ ˆํ•˜๊ฒŒ python์˜ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ ๋“ฑ์—์„œ ์ž˜ ํ™œ์šฉ๋˜๋ ค๋ฉด ์•„๋งˆ๋„ ๋‹ค์–‘ํ•œ alias๊ฐ€ ํ•„์š”ํ•  ๊ฑฐ๋ผ ๋ด…๋‹ˆ๋‹ค. ๋‹จ์ ์ธ ์˜ˆ๋กœ, Cosnole.WriteLine์€ ์‚ฌ์‹ค python์˜ print์™€ ๋น„๊ตํ•ด ๋„ˆ๋ฌด verboseํ•œ ๋Š๋‚Œ์ด ์žˆ์œผ๋‹ˆ๊นŒ์š”. (์ด๋ ‡๊ฒŒ ๋Œ€์ฒด๋กœ python์— ๋น„ํ•ด C#์€ ํƒ€์ดํ•‘์ด ๋„ˆ๋ฌด ๋งŽ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.)

ํ•˜์ง€๋งŒ, alias๋กœ ๋Œ€์ฒดํ•˜๋Š” ๊ฒƒ๋„ ํ•œ๊ณ„๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์ œ๋Œ€๋กœ ํ•œ๋‹ค๋ฉด, typescript๊ฐ€ javascipt๋กœ ๋ฒˆ์—ญํ•˜๋“ฏ์ด C#์— ๋Œ€ํ•ด์„œ๋„ typescript์ฒ˜๋Ÿผ ๊ฐ์‹ธ์ฃผ๋Š” ์–ธ์–ด๊ฐ€ ๋‚˜์˜ค๋˜๊ฐ€โ€ฆ ํ•˜๋Š” ์‹์ด์–ด์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ข‹์•„์š” 1

alias๋ณด๋‹ค๋Š” ํ™•์žฅ ๋ฉ”์†Œ๋“œ๋กœ python๊ณผ ์œ ์‚ฌํ•œ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค์ง€์•Š์„๊นŒ ์ƒ๊ฐํ•ด๋ด…๋‹ˆ๋‹ค

์ด ๊ธฐ๋Šฅ์˜ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์šฉ๋„ ์ค‘ ํ•˜๋‚˜๋Š” ๊ต์œก ์ž๋ฃŒ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ดˆ๊ธ‰ C# ๊ฐœ๋ฐœ์ž๊ฐ€ 1~2์ค„์˜ ์ฝ”๋“œ๋กœ ์ •์‹ โ€œHello World!โ€๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ๊ณต์‹ ์ ˆ์ฐจ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ˆ™๋ จ๋œ ๊ฐœ๋ฐœ์ž๋„ ์ด ๊ธฐ๋Šฅ์˜ ๋‹ค์–‘ํ•œ ์šฉ๋„๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ตœ์ƒ์œ„ ์ˆ˜์ค€ ๋ฌธ์„ ํ†ตํ•ด Jupyter Notebook์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•œ ์Šคํฌ๋ฆฝํŠธ ์œ ์‚ฌ ํ™˜๊ฒฝ์„ ์‹คํ—˜์šฉ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์ƒ์œ„ ์ˆ˜์ค€ ๋ฌธ์€ ์ž‘์€ ์ฝ˜์†” ํ”„๋กœ๊ทธ๋žจ ๋ฐ ์œ ํ‹ธ๋ฆฌํ‹ฐ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. Azure Functions๋Š” ์ตœ์ƒ์œ„ ๋ฌธ์— ์ด์ƒ์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€์ž…๋‹ˆ๋‹ค.

TLS๋ฅผ SmallSharp๊ณผ ํ•จ๊ป˜ ์“ฐ๋ฉด, ๊ฐ•์˜ ๊ฐ™์€ ๋ฐ์—์„œ ์˜ˆ์ œ ํ”„๋กœ์ ํŠธ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†์„ ๋“ฏํ•ฉ๋‹ˆ๋‹ค. ^^

SmallSharp | Create, edit and run multiple C# 9.0 top-level programs in the same project by just selecting the startup program from the start button. (clarius.org)

์ข‹์•„์š” 1