KO CSharp Keyword readonly - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
ะฃะบัะฐัะฝััะบะฐ ยท ะ ัััะบะธะน ยท English ยท Deutsch ยท Franรงais ยท Italiano ยท Espaรฑol ยท ็น้ซไธญๆ ยท ๆฅๆฌ่ช ยท ํ๊ตญ์ด
C#: readonly
ํธ๋๋ถ C# ยท ์ค์ฉ ๊ฐ์ด๋
์ด๊ธฐํ ๋๋ ์์ฑ์ ํ์ ํ๋ ๊ต์ฒด๋ฅผ ๊ธ์งํฉ๋๋ค. ๋ํ ๋ถ๋ณ ๊ตฌ์กฐ์ ์ฝ๊ธฐ ์ ์ฉ ์ฐธ์กฐ ์ก์ธ์ค์ ๋ํด์๋ ์ค๋ช ํฉ๋๋ค.
๊ฐ๋จํ ์์ (Example.cs)
์ด ์์ ๋ฅผ Example.cs๋ก ๋ฐ๋ก ์ ์ฅํ๊ณ ์ด ํ์ด์ง์ ๋ค๋ฅธ ์์ ์ ๋
๋ฆฝ์ ์ผ๋ก ์คํํ์ธ์.
Example.cs:
// PROJECT-EXAMPLE
System.Console.WriteLine(new Item(7).Count);
class Item { public readonly int Count; public Item(int count) { Count = count; } }
๊ฒฐ๊ณผ:
7
ํต์ฌ๋ก
readonly Type field; / readonly struct Type / ref readonly Type
๋ชฉ๋ก์ ๋ํ ์ฝ๊ธฐ ์ ์ฉ ์ฐธ์กฐ๋ ๋ชฉ๋ก์ ์์๋ฅผ ๋ณ๊ฒฝํ ์ ์๊ฒ ๋ง๋ค์ง ์์ต๋๋ค. const์ ๊ฒฝ์ฐ ๊ฐ์ ์ปดํ์ผ ํ์์ ์๋ ค์ ธ์ผ ํฉ๋๋ค.
์์ 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)
{
Settings settings = new("profile");
Console.WriteLine(settings.Name);
}
}
public class Settings(string name) { public readonly string Name = name; }
์ 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)
{
Position position = new(2, 3);
Console.WriteLine(position.Sum);
}
}
public readonly struct Position(int x, int y) { public int X { get; } = x; public int Y { get; } = y; public int Sum => X + Y; }
๊ณต์ ์ถ์ฒ: Microsoft ํ์ต.