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 ํ•™์Šต.