KO CSharp Keyword params - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki

ะฃะบั€ะฐั—ะฝััŒะบะฐ ยท ะ ัƒััะบะธะน ยท English ยท Deutsch ยท Franรงais ยท Italiano ยท Espaรฑol ยท ็น้ซ”ไธญๆ–‡ ยท ๆ—ฅๆœฌ่ชž ยท ํ•œ๊ตญ์–ด

C#: params

ํ•ธ๋“œ๋ถ C# ยท ์‹ค์šฉ ๊ฐ€์ด๋“œ

๋‹ค์–‘ํ•œ ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์—ด ๋˜๋Š” ์ง€์›๋˜๋Š” ์ปฌ๋ ‰์…˜ ํ˜•์‹์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”์†Œ๋“œ์˜ ๋งˆ์ง€๋ง‰ ๋งค๊ฐœ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•œ ์˜ˆ์ œ (Example.cs)

์ด ์˜ˆ์ œ๋ฅผ Example.cs๋กœ ๋”ฐ๋กœ ์ €์žฅํ•˜๊ณ  ์ด ํŽ˜์ด์ง€์˜ ๋‹ค๋ฅธ ์˜ˆ์ œ์™€ ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰ํ•˜์„ธ์š”.

Example.cs:

// PROJECT-EXAMPLE
int Sum(params int[] values)
{
    int total = 0;
    foreach (int value in values) total += value;
    return total;
}
System.Console.WriteLine(Sum(1, 2, 3));

๊ฒฐ๊ณผ:

6

ํ†ต์‚ฌ๋ก 

Method(params int[] values) / Method(params ReadOnlySpan<int> values)

params ์ปฌ๋ ‰์…˜์€ C# 13์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „ ๋ฒ„์ „์˜ ์–ธ์–ด์—๋Š” ๋Œ€๊ทœ๋ชจ ํ˜•์‹์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ 1: ๊ธฐ๋ณธ ํ˜•ํƒœ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Console.WriteLine(Numbers.Sum(1, 2, 3));
        Console.WriteLine(Numbers.Sum(new int[] { 4, 5 }));
    }
}
public static class Numbers { public static int Sum(params int[] values) => values.Sum(); }

์˜ˆ 2: ๋˜ ๋‹ค๋ฅธ ์‹ค์ œ ํ˜•์‹

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Console.WriteLine(Numbers.Sum(2, 4, 6));
    }
}
public static class Numbers { public static int Sum(params ReadOnlySpan<int> values) { int sum = 0; foreach (int value in values) sum += value; return sum; } }

๊ณต์‹ ์ถœ์ฒ˜: Microsoft ํ•™์Šต.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ