250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • MySQL
  • ใ…ฃใ„ท
  • ์œ„์ž„
  • ์ปดํฌ์ง€์…˜

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

C#/์ด๊ฒƒ์ด C#์ด๋‹ค

31. ๊ตฌ์กฐ์ฒด, ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ ๊ตฌ์กฐ์ฒด(readonly struct)

2022. 2. 14. 11:07
728x90

C# ๋ณตํ•ฉ ๋ฐ์ดํ„ฐ ํ˜•์‹์—๋Š” ํด๋ž˜์Šค ๋ง๊ณ ๋„ ๊ตฌ์กฐ์ฒด๋ผ๋Š” ๊ฒƒ์ด ์žˆ๋‹ค.

๊ตฌ์กฐ์ฒด๋Š” ํด๋ž˜์Šค์˜ ์‚ฌ์ดŒ์ง€๊ฐ„์ฏค ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•„๋“œ์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋“ฑ ์ƒ๋‹น ๋ถ€๋ถ„ ๋น„์Šทํ•˜๋‹ค.

 

struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„
{
	//ํ•„๋“œ
    //๊ตฌ์กฐ์ฒด
}

๊ตฌ์กฐ์ฒด๋ฅผ ์„ ์–ธํ•˜๋Š” ์˜ˆ

struct MyStruct
{
	public int MyField1
    public int MtField2
    
    public void MyMethod()
    {
    	//...
    }
}

<๊ตฌ์กฐ์ฒด์˜ public ํ•„๋“œ>

๊ตฌ์กฐ์ฒด์˜ ํ•„๋“œ๋ฅผ public์œผ๋กœ ์„ ์–ธํ–ˆ๋‹ค.

ํด๋ž˜์Šค์—์„œ๋Š” ์€๋‹‰์„ฑ๋•Œ๋ฌธ์— ๋ชจ๋“  ํ•„๋“œ๋ฅผ ๋ฐ˜๋“œ์‹œ private๋กœ ์ž‘์„ฑํ•˜์˜€๋‹ค.

๋ฌธ๋ฒ•์ ์œผ๋กœ๋Š” ๊ตฌ์กฐ์ฒด๊ฐ€ ํด๋ž˜์Šค์™€ ์œ ์‚ฌํ•˜๊ธฐ๋Š” ํ•ด๋„, ์กด์žฌ ์ด์œ ๊ฐ€ ๋‹ค๋ฅด๋‹ค.

 

ํด๋ž˜์Šค๋Š” ์‹ค์„ธ๊ณ„์˜ ๊ฐ์ฒด๋ฅผ ์ถ”์ƒํ™”ํ•˜๋ ค๋Š”๋ฐ ๊ทธ ์กด์žฌ์˜ ์ด์œ ๊ฐ€ ์žˆ์ง€๋งŒ

๊ตฌ์กฐ์ฒด๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

(๋”ฐ๋ผ์„œ ๊ตณ์ด ์€๋‹‰์„ฑ์„ ๋น„๋กฏํ•œ ๊ฐ์ฒด์ง€ํ–ฅ์˜ ์›์น™์„ ๊ตฌ์กฐ์ฒด์— ๊ฐ•ํ•˜๊ฒŒ ์ ์šฉํ•˜์ง€๋Š” ์•Š๋Š” ํŽธ์ด๋ฉฐ,

ํŽธ์˜๋ฅผ ์œ„ํ•ด ํ•„๋“œ๋ฅผ public์œผ๋กœ ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.)

 

ํŠน์ง• ํด๋ž˜์Šค ๊ตฌ์กฐ์ฒด
ํ‚ค์›Œ๋“œ class struct
ํ˜•์‹ ์ฐธ์กฐ ํ˜•์‹(ํž™์— ํ• ๋‹น) ๊ฐ’ ํ˜•์‹(์Šคํƒ์— ํ• ๋‹น)
๋ณต์‚ฌ ์–•์€ ๋ณต์‚ฌ(Shallow Copy) ๊นŠ์€ ๋ณต์‚ฌ(Deep Cpoy)
์ธ์Šคํ„ด์Šค ์ƒ์„ฑ new ์—ฐ์‚ฐ์ž์™€ ์ƒ์„ฑ์ž ํ•„์š” ์„ ์–ธ๋งŒ์œผ๋กœ๋„ ์ƒ์„ฑ
์ƒ์„ฑ์ž ๋งค๊ฐœ๋ณ€์ˆ˜ ์—†๋Š” ์ƒ์„ฑ์ž ์„ ์–ธ ๊ฐ€๋Šฅ ๋งค๊ฐœ๋ณ€์ˆ˜ ์—†๋Š” ์ƒ์„ฑ์ž ์„ ์–ธ ๋ถˆ๊ฐ€๋Šฅ
์ƒ์† ๊ฐ€๋Šฅ ๊ฐ’ ํ˜•์‹์ด๋ฏ€๋กœ ์ƒ์† ๋ถˆ๊ฐ€๋Šฅ

 

ํด๋ž˜์Šค๋Š” ์ฐธ์กฐ ํ˜•์‹์ด๊ณ  ๊ตฌ์กฐ์ฒด๋Š” ๊ฐ’ ํ˜•์‹์ด๋‹ค.(์ œ์ผ ์ค‘์š”ํ•œ ์ฐจ์ด)

 

-> ๋”ฐ๋ผ์„œ ๊ตฌ์กฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋Š” ์Šคํƒ์— ํ• ๋‹น๋˜๊ณ  ์ธ์Šคํ„ด์Šค๊ฐ€ ์„ ์–ธ๋œ ๋ธ”๋ก์ด ๋๋‚˜๋Š” ์ง€์ ์˜ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‚ฌ๋ผ์ง„๋‹ค.

-> ํด๋ž˜์Šค์— ๋น„ํ•ด์„œ ์„ฑ๋Šฅ์˜ ์ด์ ์ด ์žˆ๋‹ค : ์ธ์Šคํ„ด์Šค์˜ ์‚ฌ์šฉ์ด ๋๋‚˜๋ฉด ์ฆ‰์‹œ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ œ๊ฑฐ๋œ๋‹ค๋Š” ์ ,

๊ฐ€๋น„์ง€ ์ฝœ๋ ‰ํ„ฐ๋ฅผ ๋œ ๊ท€์ฐฎ๊ฒŒ ํ•œ๋‹ค๋Š” ์ 

(์˜ˆ๋ฅผ ๋“ค์–ด 3์ฐจ์› ๊ฒŒ์ž„์„ ๋งŒ๋“œ๋Š”๋ฐ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚ด์•ผ ํ•  ์ˆ˜๋งŽ์€ ์ขŒํ‘œ๋“ค์˜ ์‚ฌ์šฉ์ด ๋๋‚˜๋ฉด ์ฆ‰์‹œ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ•ด์ œ๋˜๋Š” ๊ฒƒ๊ณผ ๋ฉ”๋ชจ๋ฆฌ์— ๊ทธ๋Œ€๋กœ ๋‚จ์•„ ๊ฐ€๋น„์ง€ ์ฝœ๋ ‰ํ„ฐ๋ฅผ ๊ดด๋กญํžˆ๋Š” ๊ฒƒ๋งŒ ๋น„๊ตํ•ด๋ด๋„ ๊ตฌ์กฐ์ฒด๋Š” ์ด์ ์ด ์žˆ๋‹ค)

 

๊ตฌ์กฐ์ฒด๋Š” ๊ฐ’ ํ˜•์‹์ด๊ธฐ ๋•Œ๋ฌธ์— ํ• ๋‹น ์—ฐ์‚ฐ์ž =๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ํ•„๋“œ๊ฐ€ ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌ๋œ๋‹ค.

๋‹ค์Œ์€ ๊ตฌ์กฐ์ฒด ๋ณต์‚ฌ์˜ ์˜ˆ์ด๋‹ค.

MyStruct s;
s.MyField1 =1;
s.MtFiedl2 =2;

MtStruct t;
t = s;
s.MyField1 =3;

<์˜ˆ์ œ ์ฝ”๋“œ>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using MyExtension;

namespace MyExtension
{
    public static class IntegerExtension
    {
        public static string Append(this string myString, string appendStr)
        {
            string thisStr = myString;
            return thisStr += appendStr;
        }
        public static int Square(this int myInt)
        {
            return myInt * myInt;
        }
        public static int Power(this int myInt, int exponent)
        {
            int result = myInt;
            for (int i = 1; i < exponent; i++)
                result = result * myInt;

            return result;
        }
    }
}
namespace ExtensionMethod
{
    struct MyStruct
    {
        public int MyField1;
        public int MyField2;
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            MyStruct s;
            s.MyField1 = 1;
            s.MyField2 = 2;
            MyStruct t;
            t = s;
            s.MyField1 = 3;

            WriteLine($"s์˜ MyField1 : {s.MyField1} MyField2๋Š” : {s.MyField2}"); //s์˜ MyField1 : 3 MyField2๋Š” : 2
            WriteLine($"t์˜ MyField1 : {t.MyField1} MyField2๋Š” : {t.MyField2}"); //t์˜ MyField1 : 1 MyField2๋Š” : 2

        }
    }
}

<์˜ˆ์ œ ์ฝ”๋“œ2>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace Structure
{
    struct Point3D
    {
        public int X;
        public int Y;
        public int Z;

        public Point3D(int X, int Y, int Z) //struct๋‚ด ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋“ค์— ๋‹ค ํ• ๋‹น์„ ํ•ด์ฃผ์–ด์•ผ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋œจ์ง€ ์•Š์Œ์„ ํ™•์ธํ•˜์˜€์Œ
        {
            this.X = X;
            this.Y = Y;
            this.Z = Z;
        }

        public override string ToString()//๊ธฐ์กด System.Object ํ˜•์‹์˜ ToString() ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ
        {
            return string.Format($"{X}, {Y}, {Z}");
        }

    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Point3D p3d1; //์„ ์–ธ๋งŒ์œผ๋กœ๋„ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
            p3d1.X = 10;
            p3d1.Y = 20;
            p3d1.Z = 40;

            WriteLine(p3d1.ToString()); //10, 20, 40

            Point3D p3d2 = new Point3D(100,200,300); //์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•œ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
            Point3D p3d3 = p3d2;//๊ตฌ์กฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค์— ํ• ๋‹นํ•˜๋ฉด ๊นŠ์€ ๋ณต์‚ฌ๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค.
            p3d3.Z = 500;

            WriteLine(p3d2.ToString()); //100, 200, 300
            WriteLine(p3d3.ToString()); //100, 200, 500

        }
    }
}

๊ฐ์ฒด๋Š” ์†์„ฑ๊ณผ ๊ธฐ๋Šฅ์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

์†์„ฑ์€ ์ƒํƒœ, ๊ธฐ๋Šฅ์€ ํ–‰์œ„๋ผ๊ณ ๋„ ํ•œ๋‹ค.

๊ฐ์ฒด์˜ ์†์„ฑ์€ ํ•„๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด ํ‘œํ˜„๋˜๋ฏ€๋กœ,

๊ฐ์ฒด์˜ ์ƒํƒœ ๋˜ํ•œ ํ•„๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด ํ‘œํ˜„๋œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ƒํƒœ์˜ ๋ณ€ํ™”๋ฅผ ํ—ˆ์šฉํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ณ€๊ฒฝ๊ฐ€๋Šฅ ๊ฐ์ฒด๋ผ๊ณ  ํ•˜๋ฉฐ,

์ƒํƒœ์˜ ๋ณ€ํ™”๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฐ์ฒด๋ฅผ ๋ณ€๊ฒฝ๋ถˆ๊ฐ€๋Šฅ ๊ฐ์ฒด๋ผ๊ณ  ํ•œ๋‹ค.

๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ ๊ฐ์ฒด์˜ ํšจ์šฉ:

(1) ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ ๊ฐ„์— ๋™๊ธฐํ™”๋ฅผ ํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ๊ทธ๋žจ ์„ฑ๋Šฅ ํ–ฅ์ƒ์ด ๊ฐ€๋Šฅ

(2) ๋ฒ„๊ทธ๋กœ ์ธํ•œ ์ƒํƒœ(๋ฐ์ดํ„ฐ)์˜ ์˜ค์—ผ์„ ๋ง‰์„ ์ˆ˜ ์žˆ์Œ

(3) ํ”„๋กœ๊ทธ๋žจ์˜ ๋ฒ„๊ทธ๋ฅผ ์œ ๋ฐœ์‹œํ‚ค๋Š” ์ƒํƒœ ์˜ค์—ผ์˜ ์›์ธ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์ˆ˜๋งŽ์€ ์Šค๋ ˆ๋“œ๋ฅผ ๋””๋ฒ„๊น…ํ•ด์•ผ ํ•˜๋Š” ์ˆ˜๊ณ ์™€ ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ์ฅ์–ด์งœ์•ผ ํ•˜๋Š” ๊ดด๋กœ์›€์„ ๊ฒช์ง€ ์•Š์•„๋„ ๋จ

 

* ๊ตฌ์กฐ์ฒด๋Š” ๋ชจ๋“  ํ•„๋“œ์™€ ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋Š”, ์ฆ‰ ๋ณ€๊ฒฝ๋ถˆ๊ฐ€๋Šฅ ๊ตฌ์กฐ์ฒด๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.

(์ด์— ๋ฐ˜ํ•ด ๊ตฌ์กฐ์ฒด๋Š” ๋ณ€๊ฒฝ๋ถˆ๊ฐ€๋Šฅ์œผ๋กœ ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค)

๊ตฌ์กฐ์ฒด๋ฅผ ์„ ์–ธํ•  ๋•Œ readonly ํ‚ค์›Œ๋“œ๋งŒ ๊ธฐ์ž…ํ•˜๋ฉด ๋œ๋‹ค.

readonly struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„
{
}

readonly๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌ์กฐ์ฒด๋ฅผ ์„ ์–ธํ•˜๋ฉด, ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ํ•ด๋‹น ๊ตฌ์กฐ์ฒด์˜ ๋ชจ๋“  ํ•„๋“œ๊ฐ€ readonly๋กœ ์„ ์–ธ๋˜๋„๋ก ๊ฐ•์ œํ•œ๋‹ค.

readonly๋กœ ์„ ์–ธ๋œ ๊ตฌ์กฐ์ฒด ์•ˆ์—์„œ

readonly๋กœ ์„ ์–ธ๋˜์ง€ ์•Š์€ ํ•„๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋Š” ์ปดํŒŒ์ผ ์—๋Ÿฌ๋ฅผ ์ผ์œผํ‚จ๋‹ค.

readonly struct InnutableStruct
{
	public readonly int ImmmutableField; //OK
    public int MutableField; //์ปดํŒŒ์ผ ์—๋Ÿฌ
}

์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ์„ ์–ธ๋œ ํ•„๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋ ค๋Š” ์‹œ๋„์— ๋Œ€ํ•ด์„œ๋„ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace Structure
{ 

    readonly struct ImmmutableStruct
    {
        public readonly int ImmmutableField; //OK
        public ImmmutableStruct(int initValue)
        {
            ImmmutableField = initValue; //์ƒ์„ฑ์ž์—์„œ๋งŒ  ์ดˆ๊ธฐํ™” ๊ฐ€๋Šฅ
        }
        public void TestMethod(int testValue)
        {
            ImmmutableField = testValue; //์ปดํŒŒ์ผ ์—๋Ÿฌ. ์ƒ์„ฑ์ž์—์„œ๋งŒ  ์ดˆ๊ธฐํ™” ๊ฐ€๋Šฅ
        }
    }

    class SomeClasss
    {
        public void SomeMethod()
        {
            ImmmutableStruct imStruct = new ImmmutableStruct(123);
            imStruct.ImmmutableField = 456;//์ปดํŒŒ์ผ ์—๋Ÿฌ!
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {

        }
    }
}

<์˜ˆ์ œ 3>

์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋ถˆ๋ณ€ ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ๊ฑด๋“œ๋ฆฌ๋ ค๊ณ  ํ•˜๋ฏ€๋กœ ์ปดํŒŒ์ผ์— ์‹คํŒจํ•  ์ˆ˜๋ฐ–์— ์—†๋‹ค.

Red ๊ฐ์ฒด์˜ ๋‚˜๋จธ์ง€ ๊ฐ’์€ ์œ ์ง€ํ•˜๋ฉด์„œ G์˜ ๊ฐ’๋งŒ 100์ธ ๊ฐ์ฒด๋ฅผ ์–ป์œผ๋ ค๋ฉด?

๊ฐ„๋‹จํ•˜๋‹ค. '์ƒˆ๋กœ์šด' ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ReadOnlyStructure
{ 

    readonly struct RGBColor
    {
        public readonly byte R; //OK
        public readonly byte G; //OK
        public readonly byte B; //OK

        public RGBColor(byte r, byte g, byte b)
        {
            R = r;
            G = g;
            B = b;
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            RGBColor Red = new RGBColor(255,0,0);
            Red.G = 100;//์ปดํŒŒ์ผ ์—๋Ÿฌ
        }
    }
}

์ƒˆ๋กœ์šด ๊ฐ์ฒด

            RGBColor MyColor = new RGBColor(Red.R, Red.G, Red.B);

<์ปดํŒŒ์ผ ์—๋Ÿฌ ์˜ˆ์ œ>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ReadOnlyMethod
{ 

    struct ACsetting
    {
        public double curInCelsius; //ํ˜„์žฌ ์˜จ๋„(c)
        public double target; //ํฌ๋ง ์˜จ๋„

        public readonly double GetFahrenheit() //readonly๋กœ ํ•œ์ •ํ•œ ๋ฉ”์†Œ๋“œ์—์„œ ๊ฐ์ฒด์˜ ํ•„๋“œ๋ฅผ ๋ฐ”๊พธ๋ ค๊ณ  ํ•˜๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ
        {
            //ํ™”์”จ(F) ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ target์— ์ €์žฅ
            target = curInCelsius * 1.8 + 32;
            return target;
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            ACsetting acs;
            acs.curInCelsius = 25;
            acs.target = 25;
            WriteLine($"{acs.curInCelsius}");
            WriteLine($"{acs.target}");

        }
    }
}

        public double target; //ํฌ๋ง ์˜จ๋„

        public readonly double GetFahrenheit() //readonly๋กœ ํ•œ์ •ํ•œ ๋ฉ”์†Œ๋“œ์—์„œ ๊ฐ์ฒด์˜ ํ•„๋“œ๋ฅผ ๋ฐ”๊พธ๋ ค๊ณ  ํ•˜๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ
        {
            //ํ™”์”จ(F) ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ target์— ์ €์žฅ
            target = curInCelsius * 1.8 + 32;
            return target;
        }

 

GetFahrenheit() ํ•จ์ˆ˜ ์•ˆ์—์„œ

target = curInCelsius * 1.8 + 32; ๋ถ€๋ถ„์ด ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ์ด์œ ๋Š”

์ฝ๊ธฐ ์ „์šฉ์ธ readonly ํ•จ์ˆ˜์ธ๋ฐ target ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•ด์„œ์ด๋‹ค.

๊ทธ๋ž˜์„œ ๊ตฌํ˜„์„ 

            return curInCelsius * 1.8 + 32;๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์ •์ƒ ์ž‘๋™๋œ๋‹ค.

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ReadOnlyMethod
{

    struct ACsetting
    {
        public double curInCelsius; //ํ˜„์žฌ ์˜จ๋„(c)
        public double target; //ํฌ๋ง ์˜จ๋„

        public readonly double GetFahrenheit() //readonly๋กœ ํ•œ์ •ํ•œ ๋ฉ”์†Œ๋“œ์—์„œ ๊ฐ์ฒด์˜ ํ•„๋“œ๋ฅผ ๋ฐ”๊พธ๋ ค๊ณ  ํ•˜๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ
        {
            //ํ™”์”จ(F) ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ target์— ์ €์žฅ
            return (curInCelsius * 1.8) + 32;
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            ACsetting acs;
            acs.curInCelsius = 25;
            acs.target = 25;
            WriteLine($"{acs.GetFahrenheit()}");
            WriteLine($"{acs.target}");

        }
    }
}

 

 

728x90

'C# > ์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ด๊ฒƒ์ด C#์ด๋‹ค 7์žฅ ์—ฐ์Šต๋ฌธ์ œ ์ฐธ๊ณ   (0) 2022.02.14
32. ํŠœํ”Œ  (0) 2022.02.14
30. ํ™•์žฅ ๋ฉ”์†Œ๋“œ  (0) 2022.02.14
29. ๋ถ„ํ•  ํด๋ž˜์Šค  (0) 2022.02.11
28. ์ค‘์ฒฉ ํด๋ž˜์Šค  (0) 2022.02.11
    'C#/์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • ์ด๊ฒƒ์ด C#์ด๋‹ค 7์žฅ ์—ฐ์Šต๋ฌธ์ œ ์ฐธ๊ณ 
    • 32. ํŠœํ”Œ
    • 30. ํ™•์žฅ ๋ฉ”์†Œ๋“œ
    • 29. ๋ถ„ํ•  ํด๋ž˜์Šค
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”