KO CSharp Keyword allows - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
ะฃะบัะฐัะฝััะบะฐ ยท ะ ัััะบะธะน ยท English ยท Deutsch ยท Franรงais ยท Italiano ยท Espaรฑol ยท ็น้ซไธญๆ ยท ๆฅๆฌ่ช ยท ํ๊ตญ์ด
ํธ๋๋ถ C# ยท ์ค์ฉ ๊ฐ์ด๋
ref struct ์ ํ ์ ํ ํ์ฉ์ ์ฌ์ฉํ๋ฉด ref struct๋ฅผ ์ ๋ค๋ฆญ ํ์์ด๋ ๋ฉ์๋์ ๋ํ ์ธ์๋ก ์ ๋ฌํ ์ ์์ต๋๋ค. ๊ฐ ์ธ์๊ฐ ref ๊ตฌ์กฐ์ฒด์ผ ํ์๋ ์์ต๋๋ค. ์ด๋ฌํ T๊ฐ ์๋ ์ฝ๋๋ ๋งํฌ ์์ ๊ท์น์ ๋ฐ๋ผ์ผ ํ๋ฉฐ ๋ณต์ฑ์ ์ํํ ์ ์์ต๋๋ค. ์ ํ ๋ฐฉ์ง๋ ๋ค๋ฅธ ์ ํ ์ฌํญ ๋ค์ ์์ฑ๋ฉ๋๋ค.
์ด ์์ ๋ฅผ Example.cs๋ก ๋ฐ๋ก ์ ์ฅํ๊ณ ์ด ํ์ด์ง์ ๋ค๋ฅธ ์์ ์ ๋
๋ฆฝ์ ์ผ๋ก ์คํํ์ธ์.
Example.cs:
// PROJECT-EXAMPLE
void Ignore<T>(T value) where T : allows ref struct { }
System.Span<int> values = stackalloc int[2];
Ignore(values);
System.Console.WriteLine(values.Length);๊ฒฐ๊ณผ:
2
static int Measure<T>(T value) where T : IMeasured, allows ref struct;
static void Reset<T>(scoped ref T value) where T : allows ref struct;using System;
public interface IMeasured { int Length { get; } }
public readonly ref struct NumberView : IMeasured
{
private readonly ReadOnlySpan<int> values;
public NumberView(ReadOnlySpan<int> values) => this.values = values;
public int Length => values.Length;
}
internal static class Program
{
public static void Main()
{
ReadOnlySpan<int> values = stackalloc int[] { 4, 8, 15 };
var view = new NumberView(values);
Console.WriteLine(Measure(view));
}
private static int Measure<T>(T value) where T : IMeasured, allows ref struct
=> value.Length;
}๊ฒฐ๊ณผ:
3
using System;
internal static class Program
{
public static void Main()
{
Span<int> scratch = stackalloc int[] { 1, 2, 3 };
int counter = 7;
Reset(ref scratch);
Reset(ref counter);
Console.WriteLine($"Span length: {scratch.Length}; counter: {counter}");
}
private static void Reset<T>(scoped ref T value) where T : allows ref struct
=> value = default!;
}๊ฒฐ๊ณผ:
Span length: 0; counter: 0
๊ณต์ ์ถ์ฒ: Microsoft ํ์ต.