KO CSharp Keyword where generic type constraint - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
ะฃะบัะฐัะฝััะบะฐ ยท ะ ัััะบะธะน ยท English ยท Deutsch ยท Franรงais ยท Italiano ยท Espaรฑol ยท ็น้ซไธญๆ ยท ๆฅๆฌ่ช ยท ํ๊ตญ์ด
ํธ๋๋ถ C# ยท ์ค์ฉ ๊ฐ์ด๋
์ ํ ๋งค๊ฐ๋ณ์ ์๊ตฌ์ฌํญ(๊ธฐ๋ณธ ํด๋์ค, ์ธํฐํ์ด์ค, ํด๋์ค/๊ตฌ์กฐ์ฒด, new(), notnull, unmanaged ๋ฐ ๊ธฐํ ํ์ฉ๋๋ ์กฐํฉ)์ ์ง์ ํฉ๋๋ค.
์ด ์์ ๋ฅผ Example.cs๋ก ๋ฐ๋ก ์ ์ฅํ๊ณ ์ด ํ์ด์ง์ ๋ค๋ฅธ ์์ ์ ๋
๋ฆฝ์ ์ผ๋ก ์คํํ์ธ์.
Example.cs:
// PROJECT-EXAMPLE
T Create<T>() where T : new() => new T();
System.Console.WriteLine(Create<Item>().Name);
class Item { public string Name => "Ore"; }๊ฒฐ๊ณผ:
Ore
where T : constraint1, constraint2using 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)
{
Counter counter = Factory.Create<Counter>();
Console.WriteLine(counter.Value);
}
}
public class Counter { public int Value => 7; }
public static class Factory { public static T Create<T>() where T : class, new() => new T(); }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(Values.Sum<Number>());
}
}
public interface IValue { int Value { get; } }
public readonly struct Number : IValue { public int Value => 5; }
public static class Values { public static int Sum<T>() where T : struct, IValue { T value = new(); return value.Value; } }๊ณต์ ์ถ์ฒ: Microsoft ํ์ต.